title: Creating a socket server which allows multiple connections via threads and Java
body: <p>I am trying to adapt my simple socket server so that it can have multiple TCP connections, via multithreading, but I can't seem to get it to work. My code so far is as follows, I'm not really sure where to go from here :</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class DoSomethingWithInput implements Runnable {
     private final Socket clientSocket; //initialize in const'r
     public void run() {
  
  
       BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
          String nextline;
          while ((nextline = in.readLine())!=null) {
             System.out.println(nextline);
          } //... close socket, etc.
      }
  }
  
  
  public class Socket{
  
    public Socket() {
  }
  @Override
  public void run() {
    try {
      ServerSocket serverSocket = null;
      serverSocket = new ServerSocket(5432);
      for (;;) {
        ServerSocket serverSocket = null;
        serverSocket = new ServerSocket(5432);
        for (;;) {
          Socket clientSocket = null;
          clientSocket = serverSocket.accept();
          //delegate to new thread
          new Thread(new DoSomethingWithInput(clientSocket)).start();
        }
      }
    }catch (IOException e) {
     System.err.println("Could not listen on port: 5432.");
     System.exit(1);
  }
  }
  }
  </code></pre>
  
  <p>Would anyone be able to give me some pointers on how I could do this, and why my current implementation will not work ? I was running through the tips in the Java tutorial <a href="http://download.oracle.com/javase/tutorial/networking/sockets/examples/KKMultiServerThread.java" rel="noreferrer">http://download.oracle.com/javase/tutorial/networking/sockets/examples/KKMultiServerThread.java</a> here, but the example they give here seems to use a lot of outside sources and classes like KnockKnockProtocol etc etc.</p>
  
  <p>Would anyone be able to help me out with this?</p>
  
  <p>Thank you very much!</p>