title: Socket server that waits for message from client to read
body: <p>I have the following socket server in java,</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class MiddleSocket {
      private ServerSocket middleman;
      private int port = 1027;
      private Socket client;
  
  
      protected void createSocketServer()
      {
          try
          {
              middleman = new ServerSocket(port);
              client = middleman.accept();
              middleman.close();
              PrintWriter out = new PrintWriter(client.getOutputStream(),true);
              BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
              BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
              while(true)
              {
                  System.out.println("echo: " + in.readLine());
                  out.println("test");
              }
          }
          catch(IOException e)
          {
              System.out.println(e);
          }
      }
  
  }
  </code></pre>
  
  <p>It works correctly in how it reads from the client and everything. However, I want the socket server to only try and read when there is a message from the client since in the loop it keeps reading even when no messages from the client are received this outputs a bunch of </p>
  
  <p><code>echo: null</code></p>
  
  <p>Is there some mechanism short of a <code>wait()</code> to have the while loop to know to just sit and wait and then fire when a message is sent from the client to read that message and once that message is read and a reply is sent, to then just sit idle until the next message from the client is received?</p>