0% found this document useful (0 votes)
6K views6 pages

Ex 3.a - Echo Client and Echo Server

The document describes a Java program to implement an echo client and server using TCP sockets. The server side creates a server socket to accept client connections, reads input from the client using BufferedReader, and writes the input back to the client using PrintWriter. The client side creates a socket to connect to the server, reads input from both the server and user input, and writes to the server. When the client or server receives "bye", the connection is closed. The program is implemented in the EchoServer and EchoClient classes using TCP sockets, input/output streams, and readers/writers.

Uploaded by

NivedhithaV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6K views6 pages

Ex 3.a - Echo Client and Echo Server

The document describes a Java program to implement an echo client and server using TCP sockets. The server side creates a server socket to accept client connections, reads input from the client using BufferedReader, and writes the input back to the client using PrintWriter. The client side creates a socket to connect to the server, reads input from both the server and user input, and writes to the server. When the client or server receives "bye", the connection is closed. The program is implemented in the EchoServer and EchoClient classes using TCP sockets, input/output streams, and readers/writers.

Uploaded by

NivedhithaV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Echo client and Echo server using TCP Sockets

Aim:

To write a Java program to implement Echo Client and Echo Server using TCP sockets.

Algorithm:

Server Side:

Step -1: Start.

Step -2: Import the necessary packages.

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.

Step – 7: Close the socket.

Client Side:

Step - 1: Start.

Step - 2: Import the necessary packages.

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 - 5: Send messages to the server using getOutputStream() method.

Step – 6: Display the message that is echoed back from the server.

Step – 7: Close the socket.

Program:
EchoServer.java

import java.io.*;

import java.net.*;

public class EchoServer

public EchoServer(int pno)

try

server = new ServerSocket(pno);

catch (Exception err)

System.out.println(err);

public void serve()

try

while (true)

Socket client = server.accept();

BufferedReader r = new BufferedReader(new


InputStreamReader(client.getInputStream()));
PrintWriter w = new PrintWriter(client.getOutputStream(), true);

w.println("Welcome to the Java EchoServer. Type 'bye' to close.");

String line;

do

line = r.readLine();

if ( line != null )

w.println("Got: "+ line);

}while ( !line.trim().equals("bye") );

client.close();

catch (Exception err)

System.err.println(err);

public static void main(String[] args)

EchoServer s = new EchoServer(9999);

s.serve();

private ServerSocket server;


}

EchoClient.java

import java.io.*;

import java.net.*;

public class EchoClient

public static void main(String[] args)

try

Socket s = new Socket("10.10.16.41", 9999);

BufferedReader r = new BufferedReader(new


InputStreamReader(s.getInputStream()));

PrintWriter w = new PrintWriter(s.getOutputStream(), true);

BufferedReader con = new BufferedReader(new


InputStreamReader(System.in));

String line;

do

line = r.readLine();

if ( line != null )

System.out.println(line);

line = con.readLine();

w.println(line);

}
while ( !line.trim().equals("bye") );

catch (Exception err)

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy