Program :To implement a echo server that can handle multiple clients.
Algorithm
Server
Step 1: Start.
Step 2: Create a serversocket object.
Step 3: Create a socket object.
Step 4: Connect the socket object to DataOutputStream.
Step 5: Repeat steps 6 until ‘exit’ is typed.
Step 6: Sent message to client
Step 7: Close serversocket object
Step 8: Stop
Client
Step 1: Start
Step 2: Create a socket object.
Step 3: Get inputstream from server
Step 4: Print input from server
Step 5: Stop
Program
Server Program
import java.net.*; import java.io.*; class sthread extends Thread { Socket ssoc; DataOutputStream con; sthread(Socket so,DataOutputStream co) { super(); ssoc=so; con=co; } public void run() { DataOutputStream out=null; BufferedReader in=null; int len; System.out.println("Connection accepted at : " + ssoc); System.out.println("Server waiting for message from the client....."); try { out =new DataOutputStream(ssoc.getOutputStream()); in = new BufferedReader(new InputStreamReader(ssoc.getInputStream())); String si; while(!(si = in.readLine()).equals("exit")) { len = si.length(); for(int i=0;i<len;i++) out.write((byte)si.charAt(i)); System.out.println("From client("+ this.getName()+ ")="+si); out.write(13); out.write(10); out.flush(); } System.out.println("Disconnected : " + ssoc); out.close(); in.close(); ssoc.close(); } catch(Exception e) { System.out.println(e); } } } public class EchoServer { public static void main(String arg[]) throws IOException { ServerSocket ss= null; DataInputStream in=null; DataOutputStream con=null; sthread s=null; String str=null; try { ss= new ServerSocket(95); in =new DataInputStream(System.in); con = new DataOutputStream(System.out); } catch( IOException e) { System.out.println("Error accessing port 95 or port already in use."); System.exit(1); } System.out.println("Server is running......"); Socket ssoc=null; boolean more=true; while(more) { ssoc = ss.accept(); s=new sthread(ssoc,con); s.start(); } } } Client Program import java.net.*; import java.io.*; public class EchoClient { public static void main(String arg[]) throws IOException { Socket cls=null; BufferedReader br=null; DataInputStream in = null; DataOutputStream out = null; try { cls=new Socket(InetAddress.getLocalHost(),95); br=new BufferedReader(new InputStreamReader(cls.getInputStream())); in=new DataInputStream(System.in); out= new DataOutputStream(cls.getOutputStream()); } catch(UnknownHostException uh) { System.out.println("Don't know the server"); System.exit(0); } catch(Exception e) { System.out.println("Error : "+ e); System.exit(0); } System.out.println("Connection established with : " + cls); System.out.println("Type \"exit\" to quit....."); String inp="",si; boolean more=true; while(more) { si=in.readLine(); out.writeBytes(si); out.write(13); out.write(10); if(si.equals("exit")) break; inp=br.readLine(); System.out.println(inp); } System.out.println("Disconnected.........."); br.close(); cls.close(); } } |
Output
Window 1 (Server)
java EchoServer
Server is running……
Connection accepted at : Socket[addr=/192.168.153.1,port=1061,localport=95]
Server waiting for message from the client…..
From client(Thread-0)=ram
From client(Thread-0)=sam
From client(Thread-0)=ravi
Connection accepted at : Socket[addr=/192.168.153.1,port=1062,localport=95]
Server waiting for message from the client…..
From client(Thread-1)=tom
From client(Thread-0)=pom
From client(Thread-1)=bob
Disconnected : Socket[addr=/192.168.153.1,port=1062,localport=95]
From client(Thread-0)=son
Disconnected : Socket[addr=/192.168.153.1,port=1061,localport=95]
Window 2 (Client)
java EchoClient
Connection established with : Socket[addr=freesoft/192.168.153.1,port=95,localport=1061]
Type “exit” to exit…..
ram
ram
sam
sam
ravi
ravi
pom
pom
son
son
exit
Disconnected……….
Window 3 (Client)
java EchoClient
Connection established with : Socket[addr=freesoft/192.168.153.1,port=95,localport=1062]
Type “exit” to exit…..
tom
tom
bob
bob
exit
Disconnected……….