title: how does Netty server know when client is disconnected?
body: <p>I am making Netty Server that satisfies the following conditions:</p>
  
  <ol>
  <li>Server needs to do transaction process A, when it receives packet from its client. </li>
  <li>After finishing transaction, if it is still connected, it sends back return message to client. If not, do some rollback process B.</li>
  </ol>
  
  <p>But my problem is that when I send back to client, the Server does not know wheter it is still connected or not.<br/><br/>
  I've tried following codes to figure out its connection before sending messages. However it always succeeds even though client already closed its socket. It only fails when client process is forcibly killed <em>(e.g. Ctrl+C)</em></p>
  
  <pre><code>    final ChannelFuture cf = inboundChannel.writeAndFlush(resCodec);
  
      cf.addListener(new ChannelFutureListener() {
  
          @Override
          public void operationComplete(ChannelFuture future) {
  
              if (future.isSuccess()) {
                  inboundChannel.close();
  
              } else {
                  try {
                  // do Rollback Process B Here
              }
          }
      });
  </code></pre>
  
  <p>I thought this is because of TCP protocol. If client disconnect gracefully, then FIN signal is sent to server. So it does think writeAndFlush succeeds somehow even when it doesn't.</p>
  
  <p>So I've tried following code too, but these have the same result (always return true)</p>
  
  <pre><code>    if(inboundChannel.isActive()){
        inboundChannel.writeAndFlush(msg);
      } else{
        // do RollBack B
      }
  
      // Similar codes using inboundChannel.isOpen(), inboundChannel.isWritable()
  </code></pre>
  
  <p>Neither 'channelInactive' event nor 'Connection Reset by Peer' exception occur in my case.
  <br/><br/><br/><br/><br/>
  This is the part of Netty test client code that I used.</p>
  
  <pre><code>    public void channelActive(ChannelHandlerContext ctx) {
          ctx.writeAndFlush(message).addListener(ChannelFutureListener.CLOSE);        
      }
  </code></pre>
  
  <p><br/>
  How can I notice disconnection at the time that I want to reply?</p>