Sunteți pe pagina 1din 85

Network

Programmi
ng

TOPICS
Basic Concepts, Protocols and Terminology,

Clients, Servers and Peers,

Ports and Sockets,

The Internet and IP Addresses,

Internet Services,

URLs and DNS, TCP, UDP.

The InetAddress Class,


Using Sockets (TCP and UDP),
Network Programming with GUIs.
Example Programs.

Ports and
Sockets

Ports and
Sockets
Abstract concepts of hardware ports and sockets
that allow the programmer to make use of those
communication links.

What is a port ?
a logical connection to a computer
identified by a number in the range 1-65535
No relations with physical connections to the
computer
Ports range 1-1023 are reserved (well known
ports)

For each port supplying a service, there is a

Ports and
Sockets
What are sockets?
It is used to indicate one of the two end-points of
a communication link between two processes.
Client it will initially create a socket at its end of
the communication link.

Upon receiving the client's initial request (on a


particular port number), the server will create a
new socket at its end that will be dedicated to
communication with that particular client.

Internet Services,
URLs and DNS

What is a
protocol?
Rules governing the communication that
takes place between server and client.

For the standard services, such protocols


are made available in public documents,
usually by either the Internet Engineering
Task Force (IETF) or the World Wide Web
Consortium (W3C).

Some well-known network services

What is a URL (Uniform Resource


Locator)?
A unique identifier for any resource located
on the Internet

Structure of a URL :

1. If the file name is omitted, then the server sends a default


file

from

the

directory

specified

in

the

path

name

(index.html)
2. The 'section' part of the URL indicates a named 'anchor' in
an

HTML

document

example

<A

What is domain name?

Also known as a host name, is the userfriendly equivalent of an IP address.

In the following URL , domain name is

java.sun.com
The individual parts of a domain name don't
correspond to the individual parts of an IP
address

What is Domain Name System?

Provides a mapping between IP addresses and


domain names and is held in a distributed
database.

The

IP

address

system

and

the

DNS

are

governed by ICANN (the Internet Corporation for


Assigned Names and Numbers)

When a URL is submitted to a browser, the DNS


automatically converts the domain name part

Transmission
Control
Protocol

Why both IP and TCP? (coupled together as


TCP/IP)
IP is concerned with the routing of these packets
through an internet.
But the packets can still arrive out of sequence, be
corrupted or even not arrive at all.

So, it was

decided to place another protocol layer on top of IP.


TCP :
Allows each end of a connection to
acknowledge receipt of IP packets
Requests retransmission of lost or corrupted
packets.
Allows the packets to be rearranged into their
correct sequence at the receiving end

The 4-Layer Network Model FOR


INTERNET

User Datagram
Protocol

Why
TCP
is robust , BUT
UDP?
Overhead of providing facilities such as

confirmation of receipt

Retransmission of lost or corrupted packets

So it has relatively slow throughput, but


delivery is guaranteed.

For some applications, however, these factors are


not the most important criteria, speed is what

matters. (real video)

Such applications use User Datagram

Why
UDP?
UDP
is an unreliable protocol
1. It doesn't guarantee that each packet will
arrive;
2. It doesn't guarantee that packets will be
in the right order.
3. UDP doesn't re-send a packet if it is
missing or there is some other error, and
4.

it doesn't assemble packets into the


correct order.

Starting
Network
Programmin
g
Core package : java.net

The InetAddress
Class

The InetAddress
Handles Internet addresses both as host
Class
names and as IP addresses
Method :

getByName() (Static) :

return the Internet address of a


specified host name as an InetAddress
object.
throws

the

checked

UnknownHostException
name is not recognised

exception
if

the

host

Java programs to retrieve the IP address of the current


machine

Java programs to retrieve the IP address of the current


machine

Using
Sockets
(TCP/IP)

Setting up a server process


STEP 1: Create a ServerSocket object
ServerSocket servSock = new
ServerSocket(port_num);
port_num range = 1024-65535 (non-standard)

STEP 2: Put the server into a waiting state

returns a Socket object when a


connection is made.

Setting up a server process


STEP 3: Set up input and output streams
Following Methods of class Socket are used to get
references to streams associated with the socket
returned in step 2
1. getInputStream ()
2. getOutputStream()

For a non-GUI application, we can wrap a


Scanner object around the Input Stream object
returned by method getInputStream

Setting up a server process


STEP 3: Set up input and output streams
Similarly, we can wrap a PrintWriter object around
the

OutputStream

getOutputStream.

object

returned

by

method

Setting up a server process


STEP 4 : Set up input and output streams
We simply use method nextLine for receiving data
and method println for sending data

STEP 5: Close the connection

Setting up a server process


STEP 4 : Set up input and output streams
We simply use method nextLine for receiving data
and method println for sending data

STEP 5: Close the connection

Example

Setting up the corresponding


client
1. Establish a connection to the server.

Setting up the corresponding


client
2. Set up input and output streams
exactly the same way as the server streams
were set up

3. Send and receive data.


The Scanner object at the client end will receive
messages sent by the PrintWriter object at the
server end,

PrintWriter object at the client end will send


messages that are received by the Scanner object
at the server end

Setting up the corresponding


client
4. Close the connection
exactly the same as for the server process

Datagram (UDP)
Sockets

Datagram (UDP) Sockets

connection between client and server is not


maintained

each datagram packet is sent as an isolated


transmission whenever necessary

server does not create an individual Socket object


for each client

Instead,

DatagramPacket objects are created

and sent at both ends

Server setup

Server setup

Methods

getAddress

and

getPort

DatagramPacket object are used for this

of

our

Server setup

the data will be retrieved as a string, using an


overloaded form of the String constructor that
takes three arguments:

Server setup
Create a DatagramPacket object, using an
overloaded form of the constructor that takes
four arguments:

the byte array containing the response message;

the size of the response;

the client's address;

the client's port number

Server setup

tting up the corresponding client

tting up the corresponding client


3

tting up the corresponding client

tting up the corresponding client

twork Programming with G

The following program uses the Daytime protocol to


obtain the date and time from port 13 of userspecified host(s).
It provides a text field for input of the host name by
the user and a text area for output of the hosts
response.
There are also two buttons, one that the user presses
after entry of the host name and the other that
closes down the program.
The text area is wrapped in a JScrollPane, to cater
for long lines of output, while the buttons are laid out
on a separate panel

S-ar putea să vă placă și