ClosedChannelException

ClosedChannelException

本文介绍了异常java.nio.channels.ClosedChannelException当写入客户端通道时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用netty 3.5.8创建了游戏服务器。
第一次,从服务器发送数据到客户端没有任何问题。
但当服务器操作一段时间,当我向客户端通道写入数据时,有许多异常[java.nio.channels.ClosedChannelException]发生。



任何之前有一个例外。有没有提示来解决这个问题?
我想到缓存缓冲区导致这个。



我的代码示例如下:

  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);

异常:java.nio.channels.ClosedChannelException

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


解决方案

ClosedChannelException 只会告诉您连接已关闭,因此您发出的写请求无法完成。它通常意味着1)您的应用程序在写入消息之前关闭其他地方的连接,或2)您的对等体在读取消息之前关闭连接。如果您修复(1)和(2),您不应再看到 ClosedChannelException


I has created game server using netty 3.5.8.At first time, there is not any problem with sending data from server to client.But when server operation for a while, there are many exception [java.nio.channels.ClosedChannelException] occurred when i write data to client channel.

Any one has get this exception before. Is there any tip to fix this?I think about cache buffer cause this.

My code sample like 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)
解决方案

ClosedChannelException merely tells you that the connection has been closed so the write request you issued could not be done. It usually means either 1) your application closed the connection somewhere else before you write the message or 2) your peer closed the connection before reading your message. If you fix (1) and (2), you should not see the ClosedChannelException anymore.

这篇关于异常java.nio.channels.ClosedChannelException当写入客户端通道时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 20:53