Professional Documents
Culture Documents
Communication
by Kumudha Raimond
Introduction
Sockets are commonly used for client and server interaction.
Typical system configuration places the server on one machine, with the clients on
other machines. The clients connect to the server, exchange information, and then
disconnect.
A socket has a typical flow of events. In a connection-oriented client-to-server
model, the socket on the server process waits for requests from a client. To do
this, the server first establishes (binds) an address that clients can use to find the
server. When the address is established, the server waits for clients to request a
service.
The client-to-server data exchange takes place when a client connects to the
server through a socket. The server performs the client's request and sends the
reply back to the client.
by Kumudha Raimond
by Kumudha Raimond
by Kumudha Raimond
TCP Sockets
Server Side
• Create a ServerSocket object
• Put the server into a waiting state
• Set up input and output streams
• Send and receive data
• Close the connection (after completion of the
dialogue)
Client Side
• Establish a connection to the server
• Set up input and output streams
• Send and receive data
• Close the connection
by Kumudha Raimond
Typical flow of events for a connection-oriented
socket:
by Kumudha Raimond
by Kumudha Raimond
Socket Class
by Kumudha Raimond
Java API for TCP
Data stream abstraction
◦ enables reliable transfer (send can be blocking)
◦ marshaling/unmarshaling of data
◦ access to TCP parameters:
ReceiveBufferSize, SendBufferSize
Classes Socket and ServerSocket
◦ Socket asks for connection
◦ ServerSocket listens and returns Socket
when contacted
Port numbers
◦ explicit for ServerSocket, transparent for Socket
by Kumudha Raimond
Java API for TCP
Class ServerSocket:
close
by Kumudha Raimond
Java API for TCP
Class Socket:
connect to SocketAddress
getRemoteSocketAddress since that was chosen by the TCP system on
the other side
getInputStream, getOutputStream
use them for reading and writing
which is/may be blocking
DataInputStream, DataOutputStream:
wrapper classes for streams
have methods for marshaling/ unmarshaling
isConnected
close
by Kumudha Raimond
TCP Sockets
Setting up a server process requires five steps
by Kumudha Raimond
TCP Sockets
Setting up a server process requires five steps
by Kumudha Raimond
TCP Sockets
Setting up a client process requires FOUR steps
by Kumudha Raimond
TCP Sockets
Setting up a client process requires FOUR steps
by Kumudha Raimond
Server
by Kumudha Raimond
by Kumudha Raimond
UDP Datagram Communication
Single Datagram Message : Message
User Datagram Protocol (UDP) sent in a single packet (called datagram)
by Kumudha Raimond
UDP Datagram Communication
• Unlike TCP/IP sockets, datagram sockets are connectionless.
• That is, the connection between client and server is not maintained throughout the
duration of the dialogue.
• Instead, each datagram packet is sent as an isolated transmission whenever necessary.
• Datagram (UDP) sockets provide a faster means of transmitting data than TCP/IP
sockets, but they are unreliable.
• Since the connection is not maintained between transmissions, the server does not create
an individual Socket object for each client, as in TCP/IP example.
• A further difference from TCP/IP sockets is that, instead of a ServerSocket object, the
server creates a DatagramSocket object, as does each client when it wants to send
datagram(s) to the server.
• The final and most significant difference is that DatagramPacket objects are created
and sent at both ends, rather than simple strings
by Kumudha Raimond
UDP Datagram Single Datagram Message : Message
Communication
sent in a single packet (called datagram)
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Message size:
◦ Receiving process - specify an array of a particular size to receive a
message.
◦ If the message is too big for the array, it is truncated on arrival.
◦ The underlying IP protocol allows packet lengths of up to 216 bytes, which
includes the headers as well as the message.
◦ However, most environments impose a size restriction of 8 kilobytes.
◦ Any application requiring messages larger than the maximum must fragment
them into chunks of that size
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Blocking:
◦ Sockets normally provide non-blocking sends and blocking receives for
datagram communication.
◦ The send operation returns when it has handed the message to the underlying
UDP and IP protocols, which are responsible for transmitting it to its destination.
◦ On arrival, the message is placed in a queue for the socket that is bound to the
destination port.
◦ Messages are discarded at the destination if no process already has a socket
bound to the destination port
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Timeouts:
The receive that blocks forever is suitable for use by a server that is waiting to
receive requests from its clients.
But not appropriate that a process that has invoked a receive operation should
wait indefinitely in situations where the sending process may have crashed or the
expected message may have been lost.
Timeouts can be set on sockets.
Choosing an appropriate timeout interval is difficult, but it should be fairly large
in comparison with the time required to transmit a message.
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Receive from any:
The receive method does not specify an origin for messages. Instead, an invocation
of receive gets a message addressed to its socket from any origin. The receive
method returns the Internet address and local port of the sender, allowing the
recipient to check where the message came from. It is possible to connect a
datagram socket to a particular remote port and Internet address, in which case
the socket is only able to send messages to and receive messages from that
address
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
Failure Models
by Kumudha Raimond
UDP Datagram Communication
Issues relating to datagram communication:
◦ Datagram service (connectionless)
◦ Data may be lost
◦ Data may arrive out of sequence
◦ Checksum for data but no retransmission
◦ Bad packets dropped
by Kumudha Raimond
Use of UDP
Domain Name System, which looks up DNS names in the Internet, is
implemented over UDP.
Voice over IP (VOIP) also runs over UDP.
UDP datagrams are sometimes an attractive choice because they do not
suffer from the overheads associated with guaranteed message delivery.
There are three main sources of overhead:
◦ the need to store state information at the source and destination;
◦ the transmission of extra messages;
◦ latency for the sender.
by Kumudha Raimond
UDP Sockets
Server Side
• Create a DatagramSocket object
• Create a buffer for incoming datagrams
• Create a DatagramPacket object for the incoming datagrams
• Accept an incoming datagram
• Accept the sender's address and port from the packet
• Retrieve the data from the buffer
• Create the response datagram
• Send the response datagram
• Close the Datagram Socket
by Kumudha Raimond
UDP Sockets
Client Side
• Create a DatagramSocket object
• Create the outgoing datagram
• Send the datagram message
• Create a buffer for incoming datagrams
• Create a DatagramPacket object for the incoming datagrams
• Accept an incoming datagram
• Retrieve the data from the buffer
• Close the DatagramSocket
by Kumudha Raimond
Java API for UDP Datagrams
The Java API provides datagram
communication by means of two classes:
DatagramPacket and DatagramSocket.
This class provides a constructor that makes an instance out of an array of bytes
comprising a
message, the length of the message and the Internet address and local port
number of the destination socket.
Datagram packet
Its arguments specify an array of bytes in which to receive the message and the
length of the array.
A received message is put in the DatagramPacket together with its length and the
Internet address and port of the sending socket.
The message can be retrieved from the DatagramPacket by means of the method
getData.
The methods getPort and getAddress access the port and Internet address.
by Kumudha Raimond
Java API for UDP Datagrams
Datagram Socket
• This class supports sockets for sending and receiving UDP datagrams.
• It provides a constructor that takes a port number as its argument, for use
by processes that need to use a particular port.
• It also provides a no-argument constructor that allows the system to
choose a free local port.
• These constructors can throw a SocketException if the chosen port is
already in use or if a reserved port is specified
by Kumudha Raimond
Java API for UDP Datagrams
The class DatagramSocket rovides methods
send and receive: These methods are for transmitting datagrams between a
pair of sockets. The argument of send is an instance of DatagramPacket
containing a message and its destination. The argument of receive is an empty
DatagramPacket in which to put the message, its length and its origin. The
methods send and receive can throw IOExceptions.
setSoTimeout: This method allows a timeout to be set. With a timeout set, the
receive method will block for the time specified and then throw an
InterruptedIOException.
connect: This method is used for connecting to a particular remote port and
Internet address, in which case the socket is only able to send messages to and
receive messages from that address
by Kumudha Raimond
Java API for UDP Datagrams
Class DatagramSocket
socket constructor
◦ bound to free port if no arg
◦ arguments InetAddress, Port
send a DatagramPacket, non-blocking
receive DatagramPacket, blocking
setSoTimeout (receive blocks for time T and throw
InterruptedIOException)
close DatagramSocket
throws SocketException if port unknown or in use
connect and disconnect (!!??)
setReceiveBufferSize and setSendBufferSize
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
1. Create a DatagramSocket object
Just as for the creation of a ServerSocket object, this means supplying the
object's constructor with the port number.
For example:
DatagramSocket datagramSocket = new DatagramSocket(1234);
2. Create a buffer for incoming datagrams
This is achieved by creating an array of bytes.
For example:
byte[] buffer = new byte[256];
3. Create a DatagramPacket object for the incoming datagrams
The constructor for this object requires two arguments:
The previously-created byte array;
The size of this array.
For example:
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
7. Create the response datagram
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.
The first of these arguments is returned by the getBytes method of the
String class (acting on the desired String response).
For example:
DatagramPacket outPacket = new DatagramPacket(response.getBytes(),
response.length(),clientAddress, clientPort);
Here, response is a String variable holding the return message.
by Kumudha Raimond
UDP Sockets
Setting up a server process requires NINE steps
by Kumudha Raimond
UDP Sockets
Setting up a CLIENT process requires EIGHT steps
by Kumudha Raimond
UDP Sockets
Setting up a CLIENT process requires EIGHT steps
by Kumudha Raimond
UDP Sockets
Setting up a CLIENT process requires EIGHT steps
by Kumudha Raimond