title: Why my server socket hangs on reading the body of http post request?
body: <p><strong>Warning:</strong> This is a self answered question. I faced this problem recently and found it very tricky, though it sounds very simple. I decided to post a complete question and answer with code examples for future visitors.
  <hr/>
  I have a ServerSocket, that listens connection on port 12005. It is implemented in the simpliest way:</p>
  
  <pre><code>ServerSocket ss = new ServerSocket(12005);
  while(true) {
      executorService.submit(new SocketProcessor(ss.accept());
  }
  </code></pre>
  
  <p>Here <code>SocketProcessor</code> is just a <code>Runnable</code>, that handles incoming connections:</p>
  
  <pre><code>@Override
  public void run() {
      try {
         String msg = IOUtils.toString(s.getInputStream());
         // process msg here
      } finally {
          s.close();
      }
  }     
  </code></pre>
  
  <p>The problem is that execution hangs on <code>IOUtils.toString(InputStream is)</code> method. I thought that standard solution will work better, so I replaced it with</p>
  
  <pre><code>BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
  String line;
  while ((line = in.readLine()) != null) {
      System.out.println(line);
  }
  </code></pre>
  
  <p>The output was something like:</p>
  
  <pre><code>POST / HTTP/1.1
  Host: http://localhost:12005
  Content-Type: application/json
  //other headers
                    &lt;--- empty line
                    &lt;--- here it hangs
  </code></pre>
  
  <p>If I abort the connection on the caller side, I can see a new line in output - actually it is http body:</p>
  
  <pre><code>{"name":"alex", "hobby":"football"}  
  </code></pre>
  
  <p>I tried two different request sender implementations and they both hung when I tried to read http body. That means the problem is exactly in my server socket.<br/> What is wrong with this implementation from tutorials?</p>