title: java client server multithreaded, works for only one connection
body: <p>I did my client-server application but at the moment only one user can use it. Could you help me how to make it working for more than one user. I have the following functionality: On every two minutes counter is starting to decrease. Every user have 30 seconds to connect to the application. Every connected user should see same result with which he should make some other actions. I made it by this way at the moment. The code in the different cases is not so important. I need an advice how to make it working as a structure of the code. Thanks in advance!</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class MultiServer {
      public static void main(String[] args) throws IOException {
          ServerSocket serverSocket = null;
          boolean listening = true;
  
          try {
              serverSocket = new ServerSocket(4444);
          } catch (IOException e) {
              System.err.println("Could not listen on port: 4444.");
              System.exit(-1);
          }
  
          while (listening)
          new MultiServerThread(serverSocket.accept()).start();
  
          serverSocket.close();
      }
  }
  
  
  
  
  import java.net.*;
  import java.io.*;
  import java.util.HashMap;
  
  public class MultiServerThread extends Thread {
      private Socket socket = null;
  
      public MultiServerThread(Socket socket) {
          super("MultiServerThread");
      this.socket = socket;
      }
  
      public void run() {
          try {
  
              ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
              ObjectInputStream fromServer = new ObjectInputStream(socket.getInputStream());
  
              int userProcess = 0;
              Object data = 11111;
              boolean listening = true;
  
  
              CountDown c = new CountDown();
              int timeRemaining = 900;
  
              while (listening) {
                  boolean send = true;
                  Object ob;
                  try {
                      ob = fromServer.readObject();
                      userProcess = Integer.parseInt(ob.toString());
                      HashMap&lt;String,Integer&gt; finalScores = new HashMap&lt;String,Integer&gt;();
                      if(userProcess == 0) {
                          timeRemaining = c.getRemainingTime();
                          int temp = 999;
                          while(timeRemaining-110&gt;0) {
                              timeRemaining = c.getRemainingTime();
                              if(temp != timeRemaining) {
                                  toServer.writeObject(timeRemaining-110);
                                  toServer.flush();
                                  temp = timeRemaining;
                              }
                          } 
                      } 
                      if(userProcess == 0 &amp;&amp; timeRemaining-110 &lt; 0) {
                          c = new CountDown();
                          send = false;
                      }
  
                      if(userProcess == 1) {   
                          BoardGeneraor board = new BoardGeneraor();
                          data = board.getBoard();
                      } 
                      if(userProcess == 2) {   
                          int score = (Integer)fromServer.readObject();
                          String username = (String)fromServer.readObject();
                          finalScores.put(username, score);
                          data = finalScores;
                          c = new CountDown();
                      }
                      if(send) {
                         toServer.writeObject(data);
                          toServer.flush();
                      } else {
                          toServer.writeObject("quit");
                          toServer.flush();
                      }
                  } catch (ClassNotFoundException e) {
                      System.out.println(e);
                  }
              }
  
              fromServer.close();
              toServer.close();
              socket.close(); 
  
          } catch(IOException e) {
               System.out.println(e);
          }
      }
  }
  </code></pre>