首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >"java.io.IOException:Connection reset by peer“是什么时候抛出的?

"java.io.IOException:Connection reset by peer“是什么时候抛出的?
EN

Stack Overflow用户
提问于 2011-12-28 23:56:05
回答 5查看 198K关注 0票数 74
ERROR GServerHandler  - java.io.IOException: Connection reset by peer
java.io.IOException: Connection reset by peer
        at sun.nio.ch.FileDispatcher.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(Unknown Source)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
        at sun.nio.ch.IOUtil.read(Unknown Source)
        at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
        at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:323)
        at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:282)
        at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:202)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

此日志来自使用netty实现的游戏服务器。导致此异常的原因是什么?

EN

回答 5

Stack Overflow用户

发布于 2011-12-29 16:55:45

根据BalusC的回答,任何情况下,发送方在对等方停止读取并关闭其套接字后继续写入都会产生此异常,就像对等方在自己的套接字接收缓冲区中仍有未读数据时关闭一样。换句话说,应用程序协议错误。例如,如果您向对等方写入了对等方不理解的内容,然后它关闭了套接字以示抗议,然后您继续编写,则对等方的TCP堆栈将发出RST,这将在发送方产生此异常和消息。

票数 18
EN

Stack Overflow用户

发布于 2011-12-31 00:05:22

Netty中的java.io.IOException表示您的游戏服务器尝试向客户端发送数据,但该客户端已关闭与服务器的连接。

而且这个异常并不是唯一的!还有其他几个。参见Xitrum中的BadClientSilencer。我不得不添加它,以防止这些错误弄乱我的日志文件。

票数 3
EN

Stack Overflow用户

发布于 2015-02-21 19:11:58

对我来说有用的代码是http://rox-xmlrpc.sourceforge.net/niotut/src/NioServer.java

// remote强制关闭连接,取消

//按下选择键并关闭频道。

    private void read(SelectionKey key) throws IOException {
            SocketChannel socketChannel = (SocketChannel) key.channel();

            // Clear out our read buffer so it's ready for new data
            this.readBuffer.clear();

            // Attempt to read off the channel
            int numRead;
            try {
                numRead = socketChannel.read(this.readBuffer);
            } catch (IOException e) {
                // The remote forcibly closed the connection, cancel
                // the selection key and close the channel.
                key.cancel();
                socketChannel.close();
                return;
            }

            if (numRead == -1) {
                // Remote entity shut the socket down cleanly. Do the
                // same from our end and cancel the channel.
                key.channel().close();
                key.cancel();
                return;
            }
...
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8658118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档