Way2Java

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);
    }
  }           
}


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.

Code Snippets

Following code snippets are very useful for the programmer in coding.

1. Finding the IP Address of the system where the program is running

InetAddress add = InetAddress.getLocalHost();
System.out.println ("The system address: " + add.getHostAddress());

Getting URN from URL (name to address)

InetAddress add = InetAddress.getByName("125.252.226.27");
System.out.println ("My system name: " + add.getHostName());

2. Getting URL from URN (address to name)

InetAddress add = InetAddress.getByName("www.rediff.com");
System.out.println ("Yahoo address in number format : " + add.getHostAddress());

3. To find who is accessing the system

a) When TCP/IP protocol is used

ServerSocket sersock = new ServerSocket(4000);
Socket sock = sersock.accept();
InetAddress add = sock.getInetAddress();
System.out.println(add.getHostAddress() + " on port number " + sock.getPort());

b) When UDP protocol is used

DatagramSocket dsock = null;
DatagramPacket dpack = null;
dsock.receive(dpack);
InetAddress add = dpack.getAddress();
System.out.println(add.getHostAddress() + " on port number " + dpack.getPort());

Java comes with many classes grouped as categories. A group gives common properties. A few groups are given below.

1. Wrapper classes
2. Listeners
3. Adapters
4. Layout managers
5. Byte streams
6. Character streams
7. Print streams
8. Filter streams
9. Wrapper streams
10. Components
11. Containers
12. Thread groups
13. Exceptions