title: Java serversocket control synchronization issue
body: <p>I'm working with some java to create a small client to monitor a network. For each node on the network, I am running a client, but also a ServerSocket to listen for incoming connections. The code I have written seems to be hanging up on the ServerSocket.listen() method. I am trying to run some test code that needs to start the server socket, and after it's set up, send it some data. Essentially my problem looks like this:</p>
  
  <p>Client.java</p>
  
  <pre><code>/*snip*/
  public void listen(){
      try{
          ServerSocket server = new ServerSocket(1234);
          server.listen();
          Socket client = server.accept()
          System.out.println("got connection");
          ... do stuff
      catch(IOException ex){
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>TestClient.java</p>
  
  <pre><code>/*snip*/
  Client client = new Client();
  client.listen();
  System.out.println("client constructed, waiting for connection");
  Socket sock = new Socket("localhost", 1234);
  ... do stuff
  </code></pre>
  
  <p>Every other example of java ServerSocket places it in its own class just to loop over itself and run in an infinite loop. I'm trying to make the server execute from another main method if possible. The problem, from what I can tell, is that the client.listen() method never completes because the socket never acquires its connection. I try to establish the connection after that line of code executes in the TestClient: in eclipse, neither of the println's get printed to the output. Is there another way to write the control structure to handle this in a clever way?</p>