title: While reading from socket how to detect when the client is done sending the request?
body: <p>I am now writing a http server and I am having problem with reading from a socket.
  My problem is that the <code>inputStream</code> from the client never ends and it keeps reading until the client is closed.
  I know that the client doesn't close the connection with the server immediately after sending the http request.
  How can I quit the <code>while loop</code> when client has sent all the request data (i.e. headers + body).            </p>
  
  <pre><code>while (in.hasNextLine()) {
      String line = in.nextLine();
  
      if (line.equals("")){ // last line of request header is blank
          break; // quit while loop when last line of header is reached
      } else {
          request = request + line + "\n";
      }
  }
  </code></pre>
  
  <p>After reading comments and answer from you guys, this is what I came up with,</p>
  
  <pre><code>     is = incoming.getInputStream();
              os = incoming.getOutputStream();
              in = new Scanner(is);
              out = new DataOutputStream(os);
  
              RequestHandler rh = new RequestHandler();
              int length = 0;
              while (in.hasNextLine()) {
                  String line = in.nextLine();
                  if (line.equals("")) { // last line of request message
                                          // header is a
                                          // blank line
                      break; // quit while loop when last line of header is
                              // reached
                  }
  
                  if (line.startsWith("Content-Length: ")) { // get the
                                                              // content-length
                      int index = line.indexOf(':') + 1;
                      String len = line.substring(index).trim();
                      length = Integer.parseInt(len);
                  }
  
                  request = request + line + "\n";
              }
  
               byte[] body = new byte[length];
               int i = 0;
               while (i &lt; length) {
               byte b = in.nextByte();
               body[i] = b;
               i++;
               }
  </code></pre>
  
  <p>but, I still don't get it about reading by bytes. I can write my code to read until -1, but still stuck when there is no EOF and client is not closing connection.</p>