Implement a Multi-threading application for echo server using socket programming in JAVA.
Server Side
Implementation
Server Side
- Create a Server Class implementing the Runnable interface
- In the run method, create a data input and output stream with a client.
- In the main method write code to create a ServerSocket at a give port.
- Create a infinite loop, to accept server connection from clients and launch a thread.
- In the thread, when we catch an IOException we use return to quit. IOException occurs when the pipe is broken, that is, the client leaves.
- Additionally we have give id's to identify when a client joined and left.
Client Side
- Establish connection with server
- Create Input and output streams for interacting with the server
- Create a infinite loop to send messages to echo server
- Quit the loop on get quit as an input.
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Server implements Runnable{ int id; Socket csocket; Server(Socket csocket, int id) { this.id=id; this.csocket = csocket; } public void run() { System.out.println("Client with Id= " + id + " connected"); DataInputStream input=null; try { input = new DataInputStream(csocket.getInputStream()); } catch (IOException e) { System.out.println(e); } DataOutputStream output=null; try { output = new DataOutputStream(csocket.getOutputStream()); } catch (IOException e) { System.out.println(e); } while(true){ try { output.writeUTF(input.readUTF()); } catch (IOException e) { System.out.println("Client with Id= " + id + " exited"); return; // TODO Auto-generated catch block } } } public static void main(String[] args) { int id=1; Scanner sc=new Scanner(System.in); int pno; // TODO Auto-generated method stub System.out.println("This is the Server Program"); System.out.println("Enter the port number"); pno=sc.nextInt(); System.out.println("Attempting to create the server socket"); ServerSocket MyService=null; try { MyService = new ServerSocket(pno); System.out.println("Server Socket Created Successfully"); } catch (IOException e) { System.out.println(e); } System.out.println("Waiting for client"); while (true) { Socket sock=null; try { sock = MyService.accept(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Connected to a new Client"); new Thread(new Server(sock,id++)).start(); } } }
Client Side Code
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.PrintStream; import java.net.Socket; import java.util.*; public class Client { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String sip; int pno; // TODO Auto-generated method stub System.out.println("This is the Client Program"); System.out.println("Enter the Server ip"); sip=sc.nextLine(); System.out.println("Enter the Port Number to Use"); pno=sc.nextInt(); System.out.println("Attempting to create the Socket"); Socket MyClient=null; try { MyClient = new Socket(sip, pno); System.out.println("Socket Created Successfully"); } catch (IOException e) { System.out.println(e); } System.out.println("Attempting to create input Stream"); DataInputStream input=null; try { input = new DataInputStream(MyClient.getInputStream()); System.out.println("Input Stream Created Successfully"); } catch (IOException e) { System.out.println(e); } System.out.println("Attempting to create Ouput Stream"); DataOutputStream output=null; try { output = new DataOutputStream(MyClient.getOutputStream()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println("Output Stream created successfully"); System.out.println("Connected to Echo Server"); System.out.println("Enter the text which you want to be echoed back"); System.out.println("Type quit to exit"); while(true){ String strIn=sc.nextLine(); String copy=new String(strIn); if (copy.trim().equals("quit")){ System.out.println("Detected quit.\nTerminating Program"); break; } try { output.writeUTF(strIn); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { System.out.println(input.readUTF()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { output.close(); input.close(); MyClient.close(); } catch (IOException e) { System.out.println(e); } } }
Server Side Output
[student@localhost bin]$ java Server
This is the Server Program
Enter the port number
7562
Attempting to create the server socket
Server Socket Created Successfully
Waiting for client
Connected to a new Client
Client with Id= 1 connected
Client with Id= 1 exited
Connected to a new Client
Client with Id= 2 connected
Client with Id= 2 exited
Client Side Output
[pict@localhost bin]$ java Client
This is the Client Program
Enter the Server ip
127.0.0.1
Enter the Port Number to Use
7562
Attempting to create the Socket
Socket Created Successfully
Attempting to create input Stream
Input Stream Created Successfully
Attempting to create Ouput Stream
Output Stream created successfully
Connected to Echo Server
Enter the text which you want to be echoed back
Type quit to exit
test
test
go
go
quit
Detected quit.
Terminating Program
[student@localhost bin]$ java Client
This is the Client Program
Enter the Server ip
7562
Enter the Port Number to Use
^C
[student@localhost bin]$ java Client
This is the Client Program
Enter the Server ip
127.0.0.1
Enter the Port Number to Use
7562
Attempting to create the Socket
Socket Created Successfully
Attempting to create input Stream
Input Stream Created Successfully
Attempting to create Ouput Stream
Output Stream created successfully
Connected to Echo Server
Enter the text which you want to be echoed back
Type quit to exit
connected
connected
quit
Detected quit.
Terminating Program
No comments:
Post a Comment