I create my client-server application, studying netty along the way. How to catch the server event of closing the channel by the client? In case of emergency or forced closing of the program on the client side, the server knows about it, and issues such a log:

java.io.IOException: Соединение разорвано другой стороной at sun.nio.ch.FileDispatcherImpl.read0(Native Method) at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) at sun.nio.ch.IOUtil.read(IOUtil.java:192) at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288) at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100) at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:349) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:112) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:571) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:512) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:426) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:398) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:805) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:145) at java.lang.Thread.run(Thread.java:745) 

But if you close the client side with the method ch.close(),ch.closeFuture().sync(); then nothing happens. How to track such an event?

Thanks in advance for your help.

    2 answers 2

    There you can hang the listener on ChannelFuture :

     ChannelFuture closeFuture = channel.closeFuture(); closeFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { } }); 
    • In my Handler-e server, the lineRead0 method added this line by the example of [link] ( netty.io/4.0/api/io/netty/channel/ChannelFuture.html ). Now when the client connects, the channel closes automatically. What exactly am I doing wrong? 'ChannelFuture f = ctx.channel (). CloseFuture (); f.addListener (new ChannelFutureListener () {public void operationComplete (ChannelFuture future) {// do something}}); ' - Killersssurprise
    • @Killersssurprise Ie if you remove the addListener , then everything works? - zella
    • Well, if you remove all this design - it works as before. The ctx.channel (). CloseFuture () method itself closes the channel, as I suspect. So when we define ChannelFuture f = ctx.channel (). CloseFuture (); - we automatically close the channel? - Killersssurprise
    • @Killersssurprise is not, closeFuture () does not close the channel, but only returns the futur on which we follow the events. So it should not be affected. - zella
    • What else could be wrong then? - Killersssurprise

    Look in the direction of periodic ping clients. Netty has tools for this IdleStateHandler

     public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new IdleStateHandler(timer, 0, 0)); } 
    • Thank you, that's exactly what I was looking for. - Killersssurprise