Skip to main content

Implement Echo client and Echo server using TCP Sockets

Implement Echo client and Echo server using TCP Sockets


EchoServer.java


import java.net.*;
import java.io.*;
public class EServer
{
public static void main(String args[])
{
ServerSocket s=null; String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
{
s=new ServerSocket(9000);
}
catch(IOException e)
{}
try
{
System.out.println(e);
c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
while(true)
{
line=is.readLine();
ps.println(line);
}}
catch(IOException e)
{
System.out.println(e);
}}}




EchoClient.java


import java.net.*;
import java.io.*;
public class EClient
{
public static void main(String arg[])
{
Socket c=null; String line;
DataInputStream is,is1; PrintStream os;
try
{
InetAddress ia = InetAddress.getLocalHost(); c=new Socket(ia,9000);
}
catch(IOException e) {}
try
{
System.out.println(e);
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
while(true)
{
System.out.println("Client:");
line=is.readLine();
os.println(line);
System.out.println("Server:" + is1.readLine());
}}
catch(IOException e)
{
System.out.println("Socket Closed!");
}}}

Comments