title: TCP threaded server/client
body: <p>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?</p>
  <p>Here is what I tried so far:</p>
  <p>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?</p>
  <p>Server code:</p>
  <pre><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(&quot;TCP server is listining&quot;);
      } catch (IOException e) {
  
      System.err.println(&quot;TCP server is not listining :   &quot;+MySocket.getLocalPort());
      } 
  
      try {
          connectionSocket = MySocket.accept();
          (new Thread(new TCPserver(connectionSocket))).start();
  
          System.out.println(&quot;Is connected now : &quot;+connectionSocket.getInetAddress());
  
      } catch (IOException e) {
  
          System.err.print(&quot;Accept failed&quot;);
      }
  }
  
  
  
  public void socketProcess( )
  {
  
      try {
          inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
      } catch (IOException e) {
          System.err.println(&quot;Message from client is not reachable&quot;);
      }
  
      DataOutputStream outToClient = null;
      try {
          outToClient = new DataOutputStream(connectionSocket.getOutputStream());
      } catch (IOException e) {
          System.err.println(&quot;Message to client is not reachable&quot;);
  
      }
  
      try {
          clientMSG = inFromClient.readLine();
      } catch (IOException e) {
          System.err.println(&quot;Message from client is not readable&quot;);
      }
  
      serverRespond = clientMSG +connectionSocket.getInetAddress()+ '\n';
  
      try {
          outToClient.writeBytes(serverRespond);
      } catch (IOException e) {
          System.err.println(&quot;Message to client is not out&quot;);
      } 
  
      try {
          MySocket.close();
      } catch (IOException e1) {
  
          System.err.println(&quot;Unable to close the socket&quot;);
      }
  
  }
  
  
  
  @Override
  public void run() {
      socketProcess();
  
  } 
  </code></pre>
  <p>}</p>
  <p>And here is my Client code:</p>
  <pre><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&lt;10;i++){
          int port = 6880;
          try {
              client = new Socket(&quot;192.168.56.1&quot;, port);
          } catch (Exception e) {
              System.err.println(&quot;Couldn't connect to the port&quot; + port);
              System.exit(1);
          }
  
          DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
  
          BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
          System.out.println(&quot;Write a message to the server :&quot;);
          str = inFromUser.readLine();
  
          outToServer.writeBytes(str + '\n');
          strFromServer = inFromServer.readLine();
          System.out.println(&quot;Respond from server: &quot; + strFromServer);
          client.close();
      }
  }
  </code></pre>