/* Muti-Threaded Echo Server and Corresponding GUI Client in Java */
// EchoServer.java
import java.net.*; import java.io.*;
public class EchoServer
{
ServerSocket ss;
public EchoServer()
{
try
{
ss = new ServerSocket(12111);
}
catch(Exception e)
{
System.out.println("Error ... " + e.getMessage());
System.exit(-1);
}
System.out.println("Listening for clients.....");
int id = 0;
while(true)
{
try
{
Socket cs = ss.accept();
ClientServiceThread ct = new ClientServiceThread(cs, id++);
ct.start();
}
catch(Exception e)
{
System.out.println(" Error " + e.getMessage());
System.exit(-1);
}
}
}
public static void main (String[] args)
{
new EchoServer();
}
class ClientServiceThread extends Thread
{
Socket cs;
int m_clientID = -1;
boolean m_bRunThread = true;
ClientServiceThread(Socket s, int clientID)
{
cs = s;
m_clientID = clientID;
}
public void run()
{
BufferedReader in = null;
PrintWriter out = null;
System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " + cs.getInetAddress().getHostName());
try
{
in = new BufferedReader(new InputStreamReader(cs.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(cs.getOutputStream()));
while(m_bRunThread)
{
String clientCommand = in.readLine();
System.out.println("Client Says :" + clientCommand);
if(clientCommand.equalsIgnoreCase("exit"))
{
m_bRunThread = false;
System.out.print("Stopping client thread for client : " + m_clientID);
System.exit(-1);
}
else
{
out.println(clientCommand);
out.flush();
}
}
in.close();
out.close();
cs.close();
System.out.println("...Stopped");
}
catch(Exception e)
{
System.exit(-1);
}
}
}
}
// EchoClient.java
import java.net.*;
import java.io.*;
public class EchoClient
{
public static void main(String args[])
{
Socket s = null;
try
{
s = new Socket("localhost", 12111);
}
catch( Exception e)
{
System.out.println("Error ... " + e.getMessage());
System.exit(-1);
}
if(s == null)
System.exit(-1);
BufferedReader in = null;
PrintWriter out = null;
try
{
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String msg;
while(true)
{
System.out.print(" Enter the Message : " );
msg=br.readLine();
if(msg.compareTo("exit")==0)
{
out.println(msg);
out.flush();
break;
}
out.println(msg);
out.flush();
System.out.println(" Server Says : " + in.readLine());
}
out.close();
in.close();
s.close();
}
catch(Exception e)
{
System.out.println("Error ... " + e.getMessage());
System.exit(-1);
}
}
}
OUTPUT :
// EchoServer.java
javac EchoServer.java
java EchoServer
Listening for clients.....
Accepted Client : ID - 0 : Address - localhost
Client Says :Welcome To JCE
Client Says :I am Baran
Client Says :CSE Rocks .....
Client Says :exit
Stopping client thread for client : 0
// EchoClient.java
javac EchoClient.java
java EchoClient
Enter the Message : Welcome To JCE
Server Says : Welcome To JCE
Enter the Message : I am Baran
Server Says : I am Baran
Enter the Message : CSE Rocks .....
Server Says : CSE Rocks .....
Enter the Message : exit
If u find any flaws , Contact to M.Baran Mahamood
No comments:
Post a Comment