title: Java Socket doesn't unblock even with read timeout
body: <p>I have an application to write the request data over socket and read the response.</p>
  
  <p>Some times Host doesn't respond to request and my code blocks, even though I'm using a read timeout.</p>
  
  <p>There is no way to clear it manually and it require system to be rebooted or restart on the server handler required. </p>
  
  <p>Here is the code used. Connection appears to block at <code>objBufferedInputStream.read(..)</code></p>
  
  <pre><code>Socket clientSocket = new Socket(objInetAddress, hostPort);
  clientSocket.setKeepAlive(true);
  clientSocket.setReceiveBufferSize(8192);
  clientSocket.setSendBufferSize(8192);
  clientSocket.setSoTimeout(waitTimeBeforeSocketClose * 1000);
  objBufferedInputStream = new BufferedInputStream(clientSocket
                      .getInputStream());
  .......
  objBufferedOutputStream.write(message, 0, message.length);
  objBufferedOutputStream.flush();
  
  while (bytesLeft &gt; 0 &amp;&amp; bytesread &gt; -1) {
          try {
              bytesread = objBufferedInputStream.read(data, 4096 - bytesLeft, 1);             
          } catch (IOException e) {
              objLogger.writeException(e);
              try {
                  objBufferedInputStream.close();
              } catch (IOException e1) {
                  objLogger.writeException(e1);
  
                  return null;
              }
              return null;
          }
          bytesLeft -= bytesread;
      }
      return data;
  </code></pre>
  
  <p>}</p>
  
  <p>Please advise whether this is expected behavior when the host doesn't respond or hold the response.</p>
  
  <p>Please advise whether there is alternate approach.</p>