Implement client-server communication using UDP

Implement client-server communication using UDP

Introduction:
           With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network without requiring prior communications to set up special transmission channels or data paths. UDP uses a simple transmission model without implicit hand-shaking  for providing reliability, ordering, or data integrity. UDP's stateless nature is also useful for servers answering small queries from huge numbers of clients. Unlike TCP, UDP is compatible with packet broadcast (sending to all on local network) and multicasting (send to all subscribers).

                       Common network applications that use UDP include  the Domain Name System (DNS).

           UDP applications use datagram sockets to establish host-to-host communications. A Datagram socket is a type of Internet socket, which is the sending or receiving point for packet delivery services.[1] Each packet sent or received on a Datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may arrive in any order and might not arrive at the receiving computer.UDP broadcasts sends are always enabled on a Datagram Socket. In order to receive broadcast packets a Datagram Socket must be bound to a more specific address.

            An application binds a socket to its endpoint of data transmission, which is a combination of an IP address and a service port. A port is a software structure that is identified by the port number, a 16 bit integer value, allowing for port numbers between 0 and 65535.

Port numbers are divided into three ranges:
·         Port numbers 0 through 1023 are used for common, well-known services.
·         Port numbers 1024 through 49151 are the registered ports
·         Ports 49152 through 65535 are dynamic ports that are not officially used for any specific service, and can be used for any purpose. They are also used as ephemeral ports , from which software running on the host may randomly choose a port in order to define itself.[2] In effect, they are used as temporary ports primarily by clients when communicating with servers.

UDP Server:

import java.net.*;
import java.io.*;
public class UDPServer  {
    public static void main(String args[])  {
                        DatagramSocket aSocket = null;
                        try  {
                                    aSocket = new DatagramSocket(8117);
                                                           
                                    byte[] buffer = new byte[1000];
                                    while(true)
                                    {
                                                DatagramPacket request = new DatagramPacket(buffer, buffer.length);
                                                aSocket.receive(request);    
                                                DatagramPacket reply = new DatagramPacket(request.getData(), request.getLength(),
                                                request.getAddress(), request.getPort());
                                                aSocket.send(reply);
                                    }
                        }
                        catch (SocketException e)
                        {
                                    System.out.println("Socket: " + e.getMessage());
                        }
                        catch (IOException e)
                        {         
                                    System.out.println("IO: " + e.getMessage());
                        }
                        finally
                        {
                                    if(aSocket != null) aSocket.close();
                        }
            }
}

UDP Client:

import java.net.*;
import java.io.*;
public class UDPClient  {
   public static void main(String args[])  {
      DatagramSocket aSocket=null;
      try  {
            aSocket=new DatagramSocket();
            byte[]m=args[0].getBytes();
            InetAddress aHost=InetAddress.getByName(args[1]);
            int serverPort=8117;
            DatagramPacket request=new            DatagramPacket(m,args[0].length(),aHost,serverPort);
            aSocket.send(request);
            byte[] buffer=new byte[1000];
            DatagramPacket reply=new DatagramPacket(buffer,buffer.length);
            aSocket.receive(reply);
            System.out.println("Reply:"+new String(reply.getData()));
      }
      catch(SocketException e)
      {   System.out.println("Socket:"+e.getMessage());  }
      catch(IOException e)
      {   System.out.println("IO:"+e.getMessage());   }
      finally
      {
            if(aSocket!=null)
               aSocket.close();
      }
   }
}

UDP: OUTPUT
C:\Documents and Settings\cse4y>e:
E:\>cd Lab
E:\Lab>cd UDP
E:\Lab\UDP>javac *.java
E:\Lab\UDP>java UDPServer 127.0.0.1
C:\Documents and Settings\cse4y>e:
E:\>cd Lab
E:\Lab>cd UDP    
E:\Lab\UDP>java UDPClient hello 127.0.0.1  
Reply:hello