0% found this document useful (0 votes)
90 views3 pages

8A. TCP Echo Server & Client

The document describes an Echo Server/Client program written in Java. It contains code for an EchoServer class and EchoClient class. The EchoServer code creates a server socket on a specified port that listens for client connections. When a client connects, it receives and echoes back any message sent from the client. The EchoClient code connects to the server socket, sends a message, and prints the echoed message received back from the server. The output shows example runs of the server and client programs communicating successfully.

Uploaded by

Navin
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)
90 views3 pages

8A. TCP Echo Server & Client

The document describes an Echo Server/Client program written in Java. It contains code for an EchoServer class and EchoClient class. The EchoServer code creates a server socket on a specified port that listens for client connections. When a client connects, it receives and echoes back any message sent from the client. The EchoClient code connects to the server socket, sends a message, and prints the echoed message received back from the server. The output shows example runs of the server and client programs communicating successfully.

Uploaded by

Navin
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/ 3

PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF CSE
211416104185

TCP ECHO SERVER/CLIENT


EchoServer.java :

import java.io.*;
import java.net.*;
public class EchoServer
{
public static void main(String args[]) throws Exception
{
try
{
int Port;
BufferedReader Buf =new BufferedReader(new
InputStreamReader(System.in));
System.out.print(" Enter the Port Address : " );
Port=Integer.parseInt(Buf.readLine());
ServerSocket sok =new ServerSocket(Port);
System.out.println(" Server is Ready To Receive a Message. ");
System.out.println(" Waiting ..... ");
Socket so=sok.accept();
if(so.isConnected()==true)
System.out.println(" Client Socket is Connected Succecfully. ");
InputStream in=so.getInputStream();
OutputStream ou=so.getOutputStream();
PrintWriter pr=new PrintWriter(ou);
BufferedReader buf=new BufferedReader(new
InputStreamReader(in));
String str=buf.readLine();
System.out.println(" Message Received From Client : " + str);
System.out.println(" This Message is Forwarded To Client. ");
pr.println(str);
pr.flush();
}
catch(Exception e)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
{
System.out.println(" Error : " + e.getMessage());
}
}
}

EchoClient.java :

import java.io.*;
import java.net.*;
public class EchoClient
{
public static void main(String args[]) throws Exception
{
try {
int Port;
BufferedReader Buf =new BufferedReader(new
InputStreamReader(System.in));
System.out.print(" Enter the Port Address : " );
Port=Integer.parseInt(Buf.readLine());
Socket sok=new Socket("localhost",Port);
if(sok.isConnected()==true)
System.out.println(" Server Socket is Connected Succecfully. ");
InputStream in=sok.getInputStream();
OutputStream ou=sok.getOutputStream();
PrintWriter pr=new PrintWriter(ou);
BufferedReader buf1=new BufferedReader(new
InputStreamReader(System.in));
BufferedReader buf2=new BufferedReader(new
InputStreamReader(in));
String str1,str2;
System.out.print(" Enter the Message : ");
str1=buf1.readLine();
pr.println(str1);
pr.flush();
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
System.out.println(" Message Send Successfully. ");
str2=buf2.readLine();
System.out.println(" Message From Server : " + str2);
}
catch(Exception e)
{
System.out.println(" Error : " + e.getMessage());
}
}
}

OUTPUT:

SERVER:

Z:\>javac EchoServer.java
Z:\>java EchoServer
Enter the Port Address : 1234
Server is Ready To Receive a Message.
Waiting .....
Client Socket is Connected Succecfully.
Message Received From Client : Welcome To panimalar
This Message is Forwarded To Client.

CLIENT:

Z:\>javac EchoClient.java
Z:\>java EchoClient
Enter the Port Address : 1234
Server Socket is Connected Succecfully.
Enter the Message : Welcome To panimalar
Message Send Successfully.
Message From Server : Welcome To panimalar

You might also like