title: Java client-server / one thread multiple clients
body: <p>First of all I am not too sure how to explain that without sounding confusing so I'll try to make it simple. I have a server app that "pairs" two clients together, as part of a two player game. The way that happens is that when the first client connects to the server, the socket connection is accepted, a new ClientProtocol thread is created with that client but not started. When another client connects to the server, he is added to the previous thread and then the thread is started. The thread knows two inputstream and two outputstream, one for each client. However when I try to read from either client or server side nothing happens. This works fine for a single client in a single thread. Here is some code:</p>
  
  <p>Server side (main thread)</p>
  
  <pre><code>public static Queue&lt;ContestProtocol&gt; contesters;
  
  public static void main(String[] args) throws IOException {
      ServerSocket serverSocket = null;
      contesters = new LinkedList&lt;ContestProtocol&gt;();
  
      try {
          serverSocket = new ServerSocket(4444);
      } catch (IOException e) {
          System.err.println("Could not listen on port: 4444.");
          System.exit(-1);
      }
  
      while (true) {
          Socket socket = serverSocket.accept();
  
          ObjectInputStream ois;
          ObjectOutputStream oos;
  
          try {
              ois = new ObjectInputStream(socket.getInputStream());
              oos = new ObjectOutputStream(socket.getOutputStream());
  
              synchronized (contesters) {
                  if (contesters.size() == 0) {
                      Contester contester1 = new contester(ois, oos);
                      contesters.add(new ContestProtocol(contester1));
                  } else {
                      Contester contester2 = new Contester(ois, oos);
                      contesters.poll().hook(contester2).start();
                  }
              }
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>server side (ContestProtocol)</p>
  
  <pre><code>private Contester contester1, contester2;
  
  public ContestProtocol(Contester contester1) {
      super("ContestProtocol");
  }
  
  public ContestProtocol hook(Contester contester2) {
      this.contester2 = contester2;
      return this;
  }
  
  public void run() {
      try {
          contester1.getOOS().writeInt(-1);
          contester2.getOOS().writeInt(-2);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>server side (contester):</p>
  
  <pre><code>public Contester(ObjectInputStream ois, ObjectOutputStream oos) {
      this.ois = ois;
      this.oos = oos;
  }
  
  public ObjectInputStream getOIS() {
      return ois;
  }
  
  public ObjectOutputStream getOOS() {
      return oos;
  }
  </code></pre>
  
  <p>client side:</p>
  
  <pre><code>try {
              socket = new Socket(serverAddr, 4444);
              oos = new ObjectOutputStream(socket.getOutputStream());
              ois = new ObjectInputStream(socket.getInputStream());
              int selectedChampion = ois.readInt();
  } catch (Exception e) {
                  e.printStackTrace();
  }
  </code></pre>
  
  <p>The ContestProtocol Thread finishes executing without problem but both clients hang on readInt(). I do not understand why.</p>