Echo Server Program Using Socket Programming In Java

Echo Server Program Using Socket Programming In Java


In the program, Client keeps reading input from user and sends to the server until. GetOutputStream method is used to send the output through the socket.

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.

Biblical spiritual warfare manual. Accountability to our pastoral families is long overdue. This book identifies the 'taboo' issues good church folk ignore, and offers a godly solution to recovery, restoration and redemption. But I want to tell my story so others will be better prepared than I was.' It is a must read for every minister's wife or woman in ministry, whose calling has been silenced, buried and discounted.

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

2
4
6
8
10
12
14
16
18
20
22
importjava.util.*;
publicclassClientEcho
publicstaticvoidmain(Stringargs[])throwsException
InetAddress add=InetAddress.getByName('snrao');
DatagramSocket dsock=newDatagramSocket();
bytearr[]=message1.getBytes();
DatagramPacket dpack=newDatagramPacket(arr,arr.length,add,7);
Date sendTime=newDate();// note the time of sending the message
dsock.receive(dpack);// receive the packet
Date receiveTime=newDate();// 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

Echo Server Program Using Socket Programming In Java
2
4
6
8
10
12
14
16
18
20
22
24
importjava.util.*;
publicclassServerEcho
publicstaticvoidmain(Stringargs[])throwsException
DatagramSocket dsock=newDatagramSocket(7);
DatagramPacket dpack=newDatagramPacket(arr1,arr1.length);
while(true)
dsock.receive(dpack);
bytearr2[]=dpack.getData();
Strings2=newString(arr2,0,packSize);
System.out.println(newDate()+' '+dpack.getAddress()+' : '+dpack.getPort()+' '+s2);
}
}


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.

Pages: 12

Program :To implement a echo server that can handle multiple clients.

Algorithm

Server

Step 1: Start.
Step 2: Create a serversocket object.
Step 3: Create a socket object.
Step 4: Connect the socket object to DataOutputStream.
Step 5: Repeat steps 6 until ‘exit’ is typed.
Step 6: Sent message to client
Step 7: Close serversocket object
Step 8: Stop

Client

Step 1: Start
Step 2: Create a socket object.
Step 3: Get inputstream from server
Step 4: Print input from server
Step 5: Stop

Program

Server Program

Output

Window 1 (Server)

java EchoServer
Server is running……
Connection accepted at : Socket[addr=/192.168.153.1,port=1061,localport=95]
Server waiting for message from the client….
From client(Thread-0)=ram
From client(Thread-0)=sam
From client(Thread-0)=ravi
Connection accepted at : Socket[addr=/192.168.153.1,port=1062,localport=95]
Server waiting for message from the client….
From client(Thread-1)=tom
From client(Thread-0)=pom
From client(Thread-1)=bob
Disconnected : Socket[addr=/192.168.153.1,port=1062,localport=95]
From client(Thread-0)=son
Disconnected : Socket[addr=/192.168.153.1,port=1061,localport=95]

Window 2 (Client)

java EchoClient
Connection established with : Socket[addr=freesoft/192.168.153.1,port=95,localport=1061]
Type “exit” to exit….
ram
ram
sam
sam
ravi
ravi
pom
pom
son
son
exit
Disconnected……….

Window 3 (Client)

java EchoClient
Connection established with : Socket[addr=freesoft/192.168.153.1,port=95,localport=1062]
Type “exit” to exit….
tom
tom
bob
bob
exit
Disconnected……….

WHAT OTHERS ARE READING

Echo Server Program Using Socket Programming In Java
© 2020