I’m trying to make a threaded TCP server that accept multiple client. It’s only accepting one client. Any idea on how to make the server accepting multiple client?
Here is what I tried so far:
I changed the server code. I ran both the server and the client, but it seems that it’s only one thread is working. Should I change the ports or something?
Server code:
package tcpecho; import java.io.*; import java.net.*; class TCPserver implements Runnable { private static String clientMSG; private static String serverRespond; private static ServerSocket MySocket=null; private static Socket client; private static Socket connectionSocket = null; private BufferedReader inFromClient = null; public TCPserver(Socket client){ TCPserver.client=client;} public static void main(String[] args) { try { MySocket = new ServerSocket(6880); System.out.println("TCP server is listining"); } catch (IOException e) { System.err.println("TCP server is not listining : "+MySocket.getLocalPort()); } try { connectionSocket = MySocket.accept(); (new Thread(new TCPserver(connectionSocket))).start(); System.out.println("Is connected now : "+connectionSocket.getInetAddress()); } catch (IOException e) { System.err.print("Accept failed"); } } public void socketProcess( ) { try { inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); } catch (IOException e) { System.err.println("Message from client is not reachable"); } DataOutputStream outToClient = null; try { outToClient = new DataOutputStream(connectionSocket.getOutputStream()); } catch (IOException e) { System.err.println("Message to client is not reachable"); } try { clientMSG = inFromClient.readLine(); } catch (IOException e) { System.err.println("Message from client is not readable"); } serverRespond = clientMSG +connectionSocket.getInetAddress()+ 'n'; try { outToClient.writeBytes(serverRespond); } catch (IOException e) { System.err.println("Message to client is not out"); } try { MySocket.close(); } catch (IOException e1) { System.err.println("Unable to close the socket"); } } @Override public void run() { socketProcess(); }
}
And here is my Client code:
package task3; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.Socket; class Client { public static void main(String argv[]) throws Exception { String str; String strFromServer; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket client = null; // for(int i=0;i<10;i++){ int port = 6880; try { client = new Socket("192.168.56.1", port); } catch (Exception e) { System.err.println("Couldn't connect to the port" + port); System.exit(1); } DataOutputStream outToServer = new DataOutputStream(client.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream())); System.out.println("Write a message to the server :"); str = inFromUser.readLine(); outToServer.writeBytes(str + 'n'); strFromServer = inFromServer.readLine(); System.out.println("Respond from server: " + strFromServer); client.close(); } }
Advertisement
Answer
Right after you do this
connectionSocket = MySocket.accept();
you need to pass this connectionSocket
instance to a new Thread and call start
on that Thread.
Also, move all the connectionSocket
processing logic to your Thread class’s run
method
(this logic is currently in the main
method after this line mentioned above).
For implementing your own Thread
, you can either extend Thread
or implement Runnable
.
Check this link for some more details: Java Threads