Datagrams in Java

TCP/IP-style networking provides a serialized, predictable, reliable stream of packet data. This is not without its cost, however. TCP includes algorithms for dealing with congestion control on crowded networks, as well as pessimistic expectations about packet loss. This leads to inefficient way to transport data.
Clients and servers that communicate via a reliable channel, such as a TCP socket, have a dedicated point-to-point channel between themselves. To communicate, they establish a connection, transmit the data, and then close the connection. All data sent over the channel is received in the same order in which it was sent. This is guaranteed by the channel.
In contrast, applications that communicate via datagrams send and receive completely independent packets of information. These clients and servers do not have and do not need a dedicated point-to-point channel. The delivery of datagrams to their destinations is not guaranteed. Nor is the order of their arrival.

Datagram

A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.

Java implements datagrams on top of the UDP (User Datagram Protocol) protocol by using two classes:

  1. DatagramPacket object is the data container.
  2. DatagramSocket is the mechanism used to send or receive the DatagramPackets.

DatagramSocket defines four public constructors. They are shown here:

SocketAddress is an abstract class that is implemented by the concrete class InetSocketAddress. InetSocketAddress encapsulates an IP address with a port number. All can throw a SocketException if an error occurs while creating the socket. DatagramSocket defines many methods. Two of the most important are send( ) and receive( ), which are shown here:

The send( ) method sends packet to the port specified by packet. The receive method waits for a packet to be received from the port specified by packet and returns the result.
Other methods give you access to various attributes associated with a DatagramSocket. Here is a sampling:

Function Usage
InetAddress getInetAddress( ) If the socket is connected, then the address is returned. Otherwise, null is returned.
int getLocalPort( ) Returns the number of the local port.
int getPort( ) Returns the number of the port to which the socket is connected. It returns –1 if the socket is not connected to a port.
boolean isBound( ) Returns true if the socket is bound to an address. Returns false otherwise.
boolean isConnected( ) Returns true if the socket is connected to a server. Returns false otherwise.
void setSoTimeout(int millis) throws SocketException Sets the time-out period to the number of milliseconds passed in millis.

DatagramPacket defines several constructors. Four are shown here:

Think of the first two forms as building an “in box, ” and the second two forms as stuffing and addressing an envelope.

DatagramPacket defines several methods, including those shown here, that give access to the address and port number of a packet, as well as the raw data and its length. In general, the get methods are used on packets that are received and the set methods are used on packets that will be sent.

Function Usage
InetAddress getAddress( ) Returns the address of the source (for datagrams being received) or destination (for datagrams being sent).
byte[ ] getData( ) Returns the byte array of data contained in the datagram. Mostly used to retrieve data from the datagram after it has been received.
int getLength( ) Returns the length of the valid data contained in the byte array that would be returned from the getData( ) method. This may not equal the length of the whole byte array.
int getOffset( ) Returns the starting index of the data.
int getPort( ) Returns the port number.
void setAddress(InetAddress ipAddress) Sets the address to which a packet will be sent. The address is specified by ipAddress.
void setData(byte[ ] data) Sets the data to data, the offset to zero, and the length to number of bytes in data
void setData(byte[ ] data, int idx, int size) Sets the data to data, the offset to idx, and the length to size.
void setLength(int size) Sets the length of the packet to size.
void setPort(int port) Sets the port to port.

A Datagram Example

The following example implements a very simple networked communications client and server. Messages are typed into the window at the server and written across the network to the client side, where they are displayed.