Echo Server UDP Example Java

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

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

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 Reply

Your email address will not be published. Required fields are marked *