title: Stop Socket with timeout from waiting after data read from socket
body: <p>I am trying to create a java http server using tcp sockets. HTTP 1.1 has a timeout value that will enable the connection to be persistent and wait for a short while for possible data from the client. I am trying to implement this timer in my program by using:<code>clientSocket.setSoTimeout()</code>. Even though this will help to leave the connection open for a certain amount of time, but it will wait for that exact amount of time before allowing the next request to be read.</p>
  
  <p>For example:</p>
  
  <p>If timeout is set to 5 seconds,</p>
  
  <p>Request 1 is read. Then the socket hangs and wait until 5 seconds is over.<br />
  Request 2 is read. The socket waits until 5 seconds is up again.</p>
  
  <p>This proves to be a problem if my timeout is set to big values. This should not be the case as the request should be processed once it is received and the timeout should only expire only if no data is received throughout the specified duration.</p>
  
  <p>Can anyone advise me on how I could resolve this?</p>
  
  <p>Edit:</p>
  
  <p>For people who face a similar problem, here is my solution:</p>
  
  <p>Since the client waits until the timeout before receiving all the data, I guessed that the client does not know that all the data from the server has been received. Hence, I added a content-length field to the HTTP response packet. Now, my client no longer hangs after receiving the data. The setSoTimeout does indeed work as stated!</p>