Q. Explain what is UDP? Write a java program to implement one way communication in a network using UDP protocol? The user datagram protocol (UDP) is called a connectionless, unreliable protocol. It does not add anything to the services of IP except to provide process to process communication instead of host to host communication and also error checking. UDP is a very simple protocol using a minimum of overhead. If a process want to send a small message and does mote care much about reliability, it can use UDP. Sending a small message using UDP takes much less interaction between the sender and the receiver than TCP. User Datagram UDP packets, called user datagrams have a fixed-size header of 8 bytes and the fields are as follows. Source port number: This a port number used by the process running on the source host and having 16 bit long. Destination port number: this is used by the process running on the destination host having 16 bit long. Length : this is a 16 bit field that defines the total length of the user datagram header plus data Checksum : this field is used to detect errors over the entire user datagram (header plus data) . the checksum is described below.
The UDP checksum calculation is different from the one for IP and ICMP. Here the checksum includes three sections; a pseudo header, the UDP header and the data coming from the application layer
Java program to implement one way communication in a network using UDP protocol UDP Send import java.io.*; import java.net.*; public class UDPSend { static final int port=6010; public static void main(String args[]) throws Exception { byte[] message = new byte[1024]; int msglen = 0; String s; InetAddress address = InetAddress.getByName("localhost"); System.out.println("Write your message? (Bye for exit)"); BufferedInputStream in=new BufferedInputStream(System.in); boolean done=false; while(!done) { msglen = in.read(message,0,1024); // Initilize the packet with data and address DatagramPacket packet = new DatagramPacket(message, msglen, address, port);
// Create a socket, and send the packet through it. DatagramSocket socket = new DatagramSocket(); socket.send(packet); s = new String(message,0,msglen-2); //System.out.println("String message: "+s+" Length "+msglen-2); if (s.equals("Bye")) { done=true; } } } } UDP Receive
import java.io.*; import java.net.*; public class UDPReceive { public static void main(String args[]) throws Exception { byte[] buffer = new byte[1024]; String s; // Create a socket to listen on the port. DatagramSocket socket = new DatagramSocket(6010); for(;;) { //Constructs a DatagramPacket for receiving packets of length length. DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Wait to receive a datagram socket.receive(packet); // Convert the contents to a string s = new String(buffer, 0, packet.getLength()-2); // And display them System.out.println("UDPReceive: received from " + packet.getAddress().getHostName() + ":" + packet.getPort() + ": " + s); if(s.equals("Bye")) break; } } }
Reference 1 Forouzan A Behrouz; Data communications and Networking; The McGraw Hill Publications; 2006