Sie sind auf Seite 1von 5

 Client/Server Communication:-

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.

The User Datagram Protocol (UDP) transports packets in a connectionless manner. In


a connectionless communication, each data packet (also called datagram) is addressed and
routed individually and may arrive at the receiver in any order. For example, if process 1 on
host A sends datagrams m1 and m2 successively to process 2 on host B, the datagrams may
be transported on the network through different routes and may arrive at the destination in
any of the two orders: m1, m2 or m2, m1.

The Transmission Control Protocol (TCP) is connection-oriented and transports a


stream of data over a logical connection established between the sender and the receiver. As a
result, data sent from a sender to a receiver is guaranteed to be received in the order they
were sent. In the above example, messages m1 and m2 are delivered to process 2 on host B in
the same order they were sent from process 1 on host A.
A socket programming construct can use either UDP or TCP transport protocols.
Sockets that use UDP for transport of packets are called “datagram” sockets and sockets that
use TCP for transport are called “stream” sockets.

A socket is an endpoint of a two-way communication link between two programs


running on the network.

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

The Connectionless Datagram Socket:-

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).

At the receiving process, a DatagramSocket object must be instantiated and bound to


a local port – this port should correspond to the port number carried in the datagram packet of
the sender.

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.

Client program Server program


Create a DatagramSocket Create a DatagramSocket
object object

and bind it to any local and bind it to a specific


port; local port;

Place the data to send in Create a byte array for


a byte receiving

array; the data;

Create a DatagramPacket Create a DatagramPacket


object, object,

specifying the data array specifying the data array.


and the
Invoke the receive
receiver’s address; method of the
Key Commonly Used Methods of the DatagramSocket API:-

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:-

Msg length host serverPort

.Java supports datagram communication through the following classes:

DatagramPacket

DatagramSocket

The class DatagramPacket contains several constructors that can be used for creating packet object.

One of them is:

DatagramPacket(byte[] buf, int length, InetAddress address, int port);

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.

The key methods of DatagramPacket class are:

 byte[] getData()

Returns the data buffer.

 int getLength()

Returns the length of the data to be sent or the length of the data received.

 void setData(byte[] buf)

Sets the data buffer for this packet.

 void setLength(int length)

Sets the length for this packet.

The class DatagramSocket supports various methods that can be used for transmitting or receiving
data a datagram over the network.

The two key methods are:

 void send(DatagramPacket p)

Sends a datagram packet from this socket.

 void receive(DatagramPacket p)

Receives a datagram packet from this socket.

Das könnte Ihnen auch gefallen