Professional Documents
Culture Documents
Ex 3.a - Echo Client and Echo Server
Ex 3.a - Echo Client and Echo Server
Ex 3.a - Echo Client and Echo Server
Aim:
To write a Java program to implement Echo Client and Echo Server using TCP sockets.
Algorithm:
Server Side:
Step -3: Under EchoServer class, create a server socket to communicate with the client
using ServerSocket() constructor.
Step – 4: Accept the request from the client using accept() method.
Step – 5: Establish socket connection between client and server using BufferedReader.
Step -6: Echo the messages back to the client using PrintWriter() constructor.
Client Side:
Step - 1: Start.
Step - 3: Under EchoClient class, create a new socket with IP address of the server system
using Socket() constructor.
Step – 4: Establish socket connection between client and server using BufferedReader.
Step – 6: Display the message that is echoed back from the server.
Program:
EchoServer.java
import java.io.*;
import java.net.*;
try
System.out.println(err);
try
while (true)
String line;
do
line = r.readLine();
if ( line != null )
}while ( !line.trim().equals("bye") );
client.close();
System.err.println(err);
s.serve();
EchoClient.java
import java.io.*;
import java.net.*;
try
String line;
do
line = r.readLine();
if ( line != null )
System.out.println(line);
line = con.readLine();
w.println(line);
}
while ( !line.trim().equals("bye") );
System.err.println(err);
Output:
Result:
Thus the Java program to implement Echo Client and Server using TCP sockets is
implemented and the output verified.