gpt4 book ai didi

java - 写入客户端 channel 时出现异常 java.nio.channels.ClosedChannelException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:28 27 4
gpt4 key购买 nike

我使用 netty 3.5.8 创建了一个游戏服务器。起初,从服务器向客户端发送数据没有任何问题。但是当服务器运行一段时间后,向客户端 channel 写入数据时出现很多异常[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
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 只是告诉您连接已关闭,因此您发出的写入请求无法完成。它通常意味着:

(1) 您的应用程序在您写入消息之前关闭了其他地方的连接,或者
(2) 您的对等方在阅读您的消息之前关闭了连接。

如果您修复 (1) 和 (2),您应该不会再看到 ClosedChannelException

关于java - 写入客户端 channel 时出现异常 java.nio.channels.ClosedChannelException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14029393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com