title: SocketChannel: java.io.IOException: An existing connection was forcibly closed by the remote host
body: <p>I'm using a SocketChannel with a network socket, and have to handle the expected exception of the other end of the socket closing the channel unexpectedly.</p>
  
  <p>The problem is, I get this IOException:</p>
  
  <pre><code>java.io.IOException: An existing connection was forcibly closed by the remote host
      at sun.nio.ch.SocketDispatcher.read0(Native Method)
      at sun.nio.ch.SocketDispatcher.read(Unknown Source)
      at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
      at sun.nio.ch.IOUtil.read(Unknown Source)
      at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
      ...
  </code></pre>
  
  <p>I need to distinguish between this exception, which is expected to happen, and unexpected exceptions, so that I can log/print the unexpected ones and handle this one. But it's just an ordinary IOException with a different text message, and although I always check SocketChannel.isOpen() and SocketChannel.isConnected(), they seem to return true in this case even though the other end of the socket has been closed.</p>
  
  <p>What can/should I do?</p>