The folllowing code implements a multi-threading application for echo server in java:
EchoServer.java:
import java.io.*;
import java.net.*;
public class EchoServer implements Runnable
{
public Socket client;
public EchoServer(Socket sock)
{
this.client = sock;
}
public EchoServer(int portnum)
{
try
{
server = new ServerSocket(portnum); //create new socket
}
catch (Exception err)
{
System.out.println(err);
}
}
public void serve()
{
try
{
while (true) //wait for client connection
{
Socket client = server.accept(); //accept client connection
System.out.println("Connection accepted at :" + client);
new Thread(new EchoServer(client)).start();
}
}
catch (Exception err)
{
System.err.println(err);
}
}
public void run()
{
try
{
long threadId = Thread.currentThread().getId();
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 )
{
System.out.println("Received from Client "+ threadId +":"+line);
w.println("Echo in client: "+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("127.0.0.1", 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:
No comments:
Post a Comment