Beruflich Dokumente
Kultur Dokumente
At a basic level, network-based systems consist of a server, client, and a media for communication as
shown in Fig. 13.1. A computer running a program that makes a request for services is called client
machine. A computer running a program that offers requested services from one or more clients is called
server machine. The media for communication can be wired or wireless network.
Request
client server
ntwk
Result
Generally, programs running on client machines make requests to a program (often called as
server program) running on a server machine. They involve networking services provided by the
transport layer, which is part of the Internet software stack, often called TCP/IP (Transport Control
Protocol/Internet Protocol) stack. The transport layer comprises two types of protocols, TCP
(Transport Control Protocol) and UDP (User Datagram Protocol). The most widely used
programming interfaces for these protocols are sockets.
TCP is a connection-oriented protocol that provides a reliable flow of data between two
computers.
UDP is a protocol that sends independent packets of data, called datagrams, from one
computer to another with no guarantees about arrival and sequencing.
There are of two types of socket APIs: connectionless datagram sockets and connection-
oriented stream-mode sockets. With datagram sockets, communication occurs in the form of
discrete messages sent from the sender to receiver; whereas with stream-mode sockets, data is
transferred using the concept of a continuous data stream flowing from a source to a destination.
Socket is bound to a port number so that the TCP layer can identify the application that
data is destined to be sent. Java provides a set of classes, defined in a package called
java.net, to enable the rapid development of network applications. Key classes,
interfaces, and exceptions in java.net package simplifying the complexity involved in
creating client and server programs are:
The Classes
ContentHandler
DatagramPacket
DatagramSocket
DatagramSocketImpl
HttpURLConnection
InetAddress
MulticastSocket
ServerSocket
Socket
SocketImpl
URL
URLConnection
URLEncoder
URLStreamHandler
The Interfaces
ContentHandlerFactory
FileNameMap
SocketImplFactory
URLStreamHandlerFactor
Exceptions
BindException
ConnectException
MalformedURLException
NoRouteToHostException
ProtocolException
SocketException
UnknownHostException
UnknownServiceException
In Java, two classes are provided for the datagram socket API: (a) The
DatagramSocket class for the sockets (b) The DatagramPacket class for the packets
exchanged. A process wishing to send or receive data using the datagram socket API must
instantiate a DatagramSocket object, which is bound to a UDP port of the machine and local
to the process.
To send a datagram to another process, the sender process must instantiate a
DatagramPacketobject that carries the following information: (1) a reference to a byte array
that contains the payload data and (2) the destination address (the host ID and port number to
which the receiver process’ DatagramSocket object is bound).
To receive datagrams sent to the socket, the receiving process must instantiate a
DatagramPacket object that references a byte array and call the receive method of the
DatagramSocket object, specifying as argument, a reference to the DatagramPacket object.
The program flow in the sender and receiver process and the key methods used for
communication using connectionless sockets.
For all socket programs, the package java.net should be imported; and very often we
need to also import the java.io package to do any input/output with the sockets. Of course, for
any file access, we also need to import the java.io package.
The format of datagram packet is:-
DatagramPacket
DatagramSocket
The class DatagramPacket contains several constructors that can be used for creating packet object.
This constructor is used for creating a datagram packet for sending packets of length length to the
Specified port number on the specified host. The message to be transmitted is indicated in the first argument.
byte[] getData()
int getLength()
Returns the length of the data to be sent or the length of the data received.
The class DatagramSocket supports various methods that can be used for transmitting or receiving
data a datagram over the network.
void send(DatagramPacket p)
void receive(DatagramPacket p)