Echo Server UDP Example Java

Before going into the details of this application "Echo Server UDP Example Java" it is advised to go through the network basics, sockets and binding and UDP basics.

After studying the first application on UDP protocol, let us go to the second application Echo Server.

The Echo server sends back to the same client the message it received. That is, when the server receives a message from the client, the server echoes the message to the same client. This type of echoing is used by the network engineers to check whether the system is well connected in the network. The port number for echo server is 7.

2nd Application: Echo Server UDP Example Java

Client program – ClientEcho.java

import java.net.*;
import java.util.*;
  
public class ClientEcho
{
  public static void main( String args[] ) throws Exception 
  {
    InetAddress add = InetAddress.getByName("snrao");   
                                         
    DatagramSocket dsock = new DatagramSocket( );
    String message1 = "This is client calling";    
    byte arr[] = message1.getBytes( );  
    DatagramPacket dpack = new DatagramPacket(arr, arr.length, add, 7);
    dsock.send(dpack);                                   // send the packet
    Date sendTime = new Date();                          // note the time of sending the message
  
    dsock.receive(dpack);                                // receive the packet
    String message2 = new String(dpack.getData( ));
    Date receiveTime = new Date( );   // note the time of receiving the message
    System.out.println((receiveTime.getTime( ) - sendTime.getTime( )) + " milliseconds echo time for " + message2);
  }
}

InetAddress add = InetAddress.getByName(“snrao”);

The static getByName() method of InetAddress class returns an object of InetAddress containing the IP address that refers the computer name passed as parameter.

byte arr[] = message1.getBytes( );

The getBytes() method of String class returns the string as a byte array.

receiveTime.getTime( )

The getTime() method of Date class returns the system time in milliseconds. Observe the next screenshot which displays 0 milliseconds as both client and server systems are the same.

Client program – ServerEcho.java

import java.net.*;
import java.util.*;
 
public class ServerEcho 
{
  public static void main( String args[]) throws Exception 
  {
    DatagramSocket dsock = new DatagramSocket(7);
    byte arr1[] = new byte[150];                                
    DatagramPacket dpack = new DatagramPacket(arr1, arr1.length );
    
    while(true) 
    {
      dsock.receive(dpack);
      
      byte arr2[] = dpack.getData();
      int packSize = dpack.getLength();
      String s2 = new String(arr2, 0, packSize);

      System.out.println( new Date( ) + "  " + dpack.getAddress( ) + " : " + dpack.getPort( ) + " "+ s2);
      dsock.send(dpack);
    }
  }           
}

Echo Server UDP Example Java

Screenshot on Echo Server UDP Example Java

getAddress() and getPort() methods returns the address of the system where data is to be transported and the port number identify the process (client system) where the data is delivered. Remaining methods are explained in the earlier programs.

To execute the program, open two DOS prompts. First execute the server program and from the other DOS prompt run the client program. You can execute the client program any number of times.

Leave a Comment

Your email address will not be published.