Saturday, January 25, 2020

DatagramSocket and DatagramPacket (UDP Client server Application)

DatagramSocket and DatagramPacket 
For UDP connections DatagramPacket and DatagramSocket are two main classes available in java.net package.

UDP Sockets are less complex and have lesser overheads in comparison to TCP. Each and every DatagramPacket object contains a data buffer and the address of the remote host to send the data to, and the port number of the remote agent which is listening to DatagramSocket class. In order to enable the agent programs talk to each other over UDP connection a datagram object is created to ensure that the both communicating programs to a port on their local machines.

DatagramPacket class stuffs bytes of data into UDP packets, which are also known as datagrams. This class also enables us to unstuffy these datagram packets, when we receive them at receiver side. The DatagramSocket is the sending and receiving point of data delivery. The DatagramSocket send and receive the data in the form of datagrams by invoking the receive() method on its DatagramSocket object.

DatagramPacket class implements the mechanism to contain (it is a container for the data) data, so it can be considered as a data container. DatagramSocket class is the mechanism used to establish the communication using the UDP protocol. 

The DatagramSocket class represents a connection less socket for sending and receiving the data packets. The UDP protocol does not offer guarantee that the content will be delivered, as no acknowledgement is provided by the receiver.

A client server application with UDP connection

Client:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Client {//UDP Client

public static void main(String[] args) throws SocketException{
DatagramPacket dpac;
DatagramSocket dsoc=new DatagramSocket(1314);//socket created
byte [] b=new byte[64];
String data="no data";
System.out.println("Client up");
try {
while(true)//infinite loop
{
dpac=new DatagramPacket(b, b.length);//empty packet created
dsoc.receive(dpac);
data=new String(dpac.getData());//data received into packet
System.out.println("We received "+data);//print data
}
}catch(IOException i)
{
System.out.println("Some exception");
}
dsoc.close();//socket closed
}

}

Server

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;
public class Server {//UDP Server
public static void main(String[] args) throws SocketException{
DatagramPacket dpac;
DatagramSocket dsoc=new DatagramSocket();//no need to define port number here
System.out.println("Server up");
try {
while(true)
{
System.out.println("sending...");
Thread.sleep(1000);//wait for 1000ms
String time=new Date().toString();
byte b[]=time.getBytes();
dpac=new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 1314);
dsoc.send(dpac);//packet send
}
}catch (IOException | InterruptedException e) {//handle exceptions
e.printStackTrace();
}
dsoc.close();
}

}

Video Tutorial