title: Java Server socket TCP connection stuck in CLOSE_WAIT state while client shutdown or client restart
body: <p>While performing Client restart or shutdown,all socket connections are hang in CLOSE_WAIT on server side.
  XdrAble used to send serialized data into XDR streams.
  pool is object of standard KeyedSocketPool.
  MAXIMUM_RETRY,MAXIMUM_RETYR_BEFORE_RESET_POOL are constants defined to reset the pool connections.
  What I am missing while closing the connections?</p>
  
  <p>public  RESP call (REQ request,
        RESP response, int requestLength, int timeOut) throws Exception
    {<br>
      Socket socket = null;    </p>
  
  <pre><code>IOException ioException = null;
  
  short attempts = 0;
  boolean needResetPool = false;
  
  while (true)
  {
    ioException = null;
  
    try
    {
      // Get socket (open if not connected)
      socket = (Socket) pool.borrowObject ();
      socket.setSoTimeout (timeOut);
  
      // Send request
      BufferedOutputStream outputStream = new BufferedOutputStream (socket
          .getOutputStream ());
      XdrBufferEncodingStream xdrIn = new XdrBufferEncodingStream (
          requestLength);
      xdrIn.beginEncoding (socket.getInetAddress (), socket.getPort ());
      request.xdrEncode (xdrIn);
      xdrIn.endEncoding ();
      outputStream.write (xdrIn.getXdrData ());
      outputStream.flush ();
      xdrIn.close ();
  
      // Read response
      BufferedInputStream inputStream = new BufferedInputStream (socket
          .getInputStream ());
      byte[] buffer = new byte[UreMsgBodyLength.MAXIMUM_MSG_LEN];
      int bytesRead = inputStream.read (buffer);
  
      if (bytesRead &lt; UreMsgBodyLength.MESSAGE_HEADER_LEN)
        throw new IOException("Socket Read Failed - invalid bytesRead="+bytesRead);
  
  
      int encodedLength = bytesRead;
      if ( encodedLength &lt; 0 || ( encodedLength &amp; 3) != 0 )
        encodedLength = UreMsgBodyLength.MAXIMUM_MSG_LEN;
  
     XdrBufferDecodingStream xdrOut = new XdrBufferDecodingStream (buffer, encodedLength);
      xdrOut.beginDecoding ();
      response.xdrDecode (xdrOut);
      xdrOut.endDecoding ();
      xdrOut.close ();
      return response;
    }
    catch (ConnectException ex)
    {
  
      ioException = ex;
    }
    catch (IOException ex)
    {
  
      ioException = ex;
  
      if (socket != null)
      {
        needResetPool = true;
        try { socket.close (); } catch (Throwable t) {}
  
        try
        {
          pool.invalidateObject (socket);          // Mark the socket as invalid
  
        }
        catch (Throwable t)
        {
  
        }
        socket = null;
      }
    }
    finally
    {    
      if (socket != null)
      {
        try
        {
          pool.returnObject (socket);
        }
        catch (Throwable t)
        {
  
        }
      }
    }
  
    if (attempts &gt;= MAXIMUM_RETYR_BEFORE_RESET_POOL &amp;&amp; needResetPool)
    {
  
      pool.clear (pool.getKey ()); // clean all connect for the current server id
    }
    if (attempts &gt;= MAXIMUM_RETRY)
    {
  
      throw ioException;
    }
  
    attempts++;
  
  } // while
  </code></pre>
  
  <p>}</p>