sockets - how does Netty server know when client is disconnected? -


i making netty server satisfies following conditions:

  1. server needs transaction process a, when receives packet client.
  2. after finishing transaction, if still connected, sends return message client. if not, rollback process b.

but problem when send client, server not know wheter still connected or not.

i've tried following codes figure out connection before sending messages. succeeds though client closed socket. fails when client process forcibly killed (e.g. ctrl+c)

    final channelfuture cf = inboundchannel.writeandflush(rescodec);      cf.addlistener(new channelfuturelistener() {          @override         public void operationcomplete(channelfuture future) {              if (future.issuccess()) {                 inboundchannel.close();              } else {                 try {                 // rollback process b here             }         }     }); 

i thought because of tcp protocol. if client disconnect gracefully, fin signal sent server. think writeandflush succeeds somehow when doesn't.

so i've tried following code too, these have same result (always return true)

    if(inboundchannel.isactive()){       inboundchannel.writeandflush(msg);     } else{       // rollback b     }      // similar codes using inboundchannel.isopen(), inboundchannel.iswritable() 

neither 'channelinactive' event nor 'connection reset peer' exception occur in case.




part of netty test client code used.

    public void channelactive(channelhandlercontext ctx) {         ctx.writeandflush(message).addlistener(channelfuturelistener.close);             } 


how can notice disconnection @ time want reply?

may should override below method , see if control goes here when channel closed.

@override public void channelinactive(channelhandlercontext ctx) throws exception {     // write cleanup code } 

i don't think possible track whether client connected or not in netty because there abstraction between netty , client.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -