Open In App

Datagrams in Java

Last Updated : 24 May, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • Datagrams plays a vital role as an alternative.
  • Datagrams are bundles of information passed between machines. Once the datagram has been released to its intended target, there is no assurance that it will arrive or even that someone will be there to catch it.
  • Likewise, when the datagram is received, there is no assurance that it hasn’t been damaged in transit or that whoever sent it is still there to receive a response and it is crucial point to note.

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 Class

DatagramSocket defines four public constructors. They are shown here:

  • DatagramSocket( ) throws SocketException : It creates a DatagramSocket bound to any unused port on the local computer.
  • DatagramSocket(int port) throws SocketException : It creates a DatagramSocket bound to the port specified by port.
  • DatagramSocket(int port, InetAddress ipAddress) throws SocketException : It constructs a DatagramSocket bound to the specified port and InetAddress.
  • DatagramSocket(SocketAddress address) throws SocketException : It constructs a DatagramSocket bound to the specified SocketAddress.

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:

  • void send(DatagramPacket packet) throws IOException
  • void receive(DatagramPacket packet) throws IOException

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 Class

DatagramPacket defines several constructors. Four are shown here:

  • DatagramPacket(byte data[ ], int size) : It specifies a buffer that will receive data and the size of a packet. It is used for receiving data over a DatagramSocket
  • DatagramPacket(byte data[ ], int offset, int size) : It allows you to specify an offset into the buffer at which data will be stored.
  • DatagramPacket(byte data[ ], int size, InetAddress ipAddress, int port) : It specifies a target address and port, which are used by a DatagramSocket to determine where the data in the packet will be sent.
  • DatagramPacket(byte data[ ], int offset, int size, InetAddress ipAddress, int port) : It transmits packets beginning at the specified offset into the data.

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.




// Java program to illustrate datagrams
import java.net.*;
class WriteServer {
  
    // Specified server port
    public static int serverPort = 998;
  
    // Specified client port
    public static int clientPort = 999;
  
    public static int buffer_size = 1024;
    public static DatagramSocket ds;
  
    // an array of buffer_size
    public static byte buffer[] = new byte[buffer_size];
  
    // Function for server
    public static void TheServer() throws Exception
    {
        int pos = 0;
        while (true) {
            int c = System.in.read();
            switch (c) {
            case -1:
  
                // -1 is given then server quits and returns
                System.out.println("Server Quits.");
                return;
            case '\r':
                break; // loop broken
            case '\n':
                // send the data to client
                ds.send(new DatagramPacket(buffer, pos,
                                           InetAddress.getLocalHost(), clientPort));
                pos = 0;
                break;
            default:
                // otherwise put the input in buffer array
                buffer[pos++] = (byte)c;
            }
        }
    }
  
    // Function for client
    public static void TheClient() throws Exception
    {
        while (true) {
  
            // first one is array and later is its size
            DatagramPacket p = new DatagramPacket(buffer, buffer.length);
  
            ds.receive(p);
  
            // printing the data which has been sent by the server
            System.out.println(new String(p.getData(), 0, p.getLength()));
        }
    }
  
    // main driver function
    public static void main(String args[]) throws Exception
    {
  
        // if WriteServer 1 passed then this will run the server function
        // otherwise client function will run
        if (args.length == 1) {
            ds = new DatagramSocket(serverPort);
            TheServer();
        }
        else {
            ds = new DatagramSocket(clientPort);
            TheClient();
        }
    }
}


This sample program is restricted by the DatagramSocket constructor to running between two ports on local machine. To use the program, run

java WriteServer

in one window; this will be the client. Then run

java WriteServer 1

This will be the server. Anything that is typed in the server window will be sent to the client window after a newline is received.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads