Java.nio.channels.ClosedChannelException exception while writing to client channel

I created a game server using netty 3.5.8. Firstly, there is no problem sending data from the server to the client. But when the server has been running for a while, there are many exceptions [ java.nio.channels.ClosedChannelException ] when I write data to the client channel.

Anyone got this exception before. Is there any advice to fix this? I am thinking about buffer caching.

An example of my code is this:

 ChannelBuffer bff = ChannelBuffers.buffer(18); bff.writeByte(Events.S_SERVER_PUSH); bff.writeByte((byte)0); bff.writeInt(idRoom); bff.writeInt(playerCnt); bff.writeInt(gameCnt); bff.writeInt(freePlayer); channel.write(bff); 

Exception: java.nio.channels.ClosedChannelException

 java.nio.channels.ClosedChannelException at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:784) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:507) at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:129) at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:66) at org.jboss.netty.channel.Channels.write(Channels.java:733) at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.doEncode(OneToOneEncoder.java:71) at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:60) at org.jboss.netty.channel.Channels.write(Channels.java:712) at org.jboss.netty.channel.Channels.write(Channels.java:679) at org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:248) at myclass.SendPushData(GameRoom.java:231) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:458) at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:439) at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:84) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:471) at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:332) at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 
+6
source share
2 answers

ClosedChannelException simply tells you that the connection was closed, so the write request you issued cannot be executed. This usually means:

(1) your application closed the connection somewhere else before you wrote a message or
(2) your colleague closed the connection before reading your message.

If you fix (1) and (2), you will no longer need to see a ClosedChannelException .

+7
source

ClosedChannelException means that you closed the channel and continued to use it. This is a programming error on your part.

+3
source

All Articles