title: How can I use multi-threading to allow multiple clients on to a server?
body: <p>I have two java classes, one for a server and one for a client. The connection is established between both of them using a regular socket. How can I allow many instances of the client class to connect to the server simultaneously using multi-threading?
  I tried searching on SO but I couldn't really find any concise/clear answers.</p>
  
  <p>Here are my important methods (both of them are in the Server class):</p>
  
  <pre><code>public void startRunning() {
  
      try {
          server = new ServerSocket(portNum, 10); // port num and backlog
  
          while (true) {
              try {
  
                  waitForConnection();
                  setupStreams();  //sets up streams
                  whileChatting(); //exchanges messages
              } catch (EOFException e1) {
                  showMessage("\n Server ended the connection");
  
              } finally {
                  closeEverything();  //closes all streams
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  }
  
  private void waitForConnection() throws IOException {
  
      showMessage(" Waiting for connections...\n");
      connection = server.accept();
      showMessage(" Connected to "
              + connection.getInetAddress().getHostName());
  
  }
  </code></pre>