title: ServerSocket accept continues to block
body: <p>I'm having some trouble simulating a connection to my Server Socket, accept seems to continue blocking as it doesn't "see" the connection.</p>
  
  <p>Here's some simplified code</p>
  
  <pre><code>@Test
  public void testPDMServerThread() {
      try {           
          ServerSocket serverSocket = new ServerSocket(0);        
          int port = serverSocket.getPort();
  
          Socket clientSocket = new Socket("localhost", port);
          PrintWriter clientRequest = new PrintWriter(clientSocket.getOutputStream(), true);
          BufferedReader serverResponse = new BufferedReader(
                  new InputStreamReader(clientSocket.getInputStream()));
          serverThread = new ProducerMonitorServerThread(serverSocket.accept());
  
          clientRequest.write("Hi!");
          serverThread.start();
          System.out.println("Server says: " + serverResponse.readLine());
          assertEquals("RUNNABLE", serverThread.getState().toString());
      } catch (IOException e) {
      }
  }
  </code></pre>
  
  <p>And here's the thread where the server should respond</p>
  
  <pre><code>public class ProducerMonitorServerThread extends Thread {
  
      private Socket socket;
  
      public ProducerMonitorServerThread(Socket socket) {
          super("PDM");
          this.socket = socket;
      }
  
      @Override
      public void run() {
  
          try {
              PrintWriter serverResponse = new PrintWriter(socket.getOutputStream(), true);
              BufferedReader clientRequest = new BufferedReader(
                      new InputStreamReader(socket.getInputStream()));
  
              String serverInput, clientOutput;
  
              while((serverInput = clientRequest.readLine()) != null) {
                  clientOutput = "Bye!";
                  System.out.println("Client says: " +serverInput);
                  serverResponse.write(clientOutput);
              }
  
              serverResponse.close();
              clientRequest.close();
              socket.close();
  
          } catch (IOException e) {
          }           
      }    
  }
  </code></pre>
  
  <p>It never seems to get past this line which is why I think accept is not seeing the connection</p>
  
  <pre><code>serverThread = new ProducerMonitorServerThread(testServer.accept());
  </code></pre>
  
  <p>I'm sure there's something fundamental that I'm just not seeing.</p>