如何通过对等方复制连接重置?

问题描述

我的服务器在日志中打印了以下内容

Caused by: java.io.IOException: Connection reset by peer
        at sun.nio.ch.FiledispatcherImpl.write0(Native Method)
        at sun.nio.ch.socketdispatcher.write(Socketdispatcher.java:47)
        at sun.nio.ch.IoUtil.writeFromNativeBuffer(IoUtil.java:93)
        at sun.nio.ch.IoUtil.write(IoUtil.java:65)
        at sun.nio.ch.socketChannelImpl.write(SocketChannelImpl.java:471)
        at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:134)
        at org.apache.tomcat.util.net.NioBlockingSelector.write(NioBlockingSelector.java:101)
        at org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:157)
        at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.doWrite(NioEndpoint.java:1238)
        at org.apache.tomcat.util.net.socketWrapperBase.doWrite(SocketWrapperBase.java:670)
        at org.apache.tomcat.util.net.socketWrapperBase.flushBlocking(SocketWrapperBase.java:607)
        at org.apache.tomcat.util.net.socketWrapperBase.flush(SocketWrapperBase.java:597)
        at org.apache.coyote.http11.Http11OutputBuffer.flushBuffer(Http11OutputBuffer.java:519)
        at org.apache.coyote.http11.Http11OutputBuffer.flush(Http11OutputBuffer.java:260)
        at org.apache.coyote.http11.Http11Processor.flush(Http11Processor.java:1494)
        at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:279)
        at org.apache.coyote.Response.action(Response.java:168)
        at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:317)

我知道客户端出于某种原因关闭了连接,但服务器仍在写入数据。而此时,客户端会返回重置标志“RST”,服务器的日志会得到Connection reset by peer

为了复制它,我让服务器休眠几秒钟,并创建了一个客户端:

 private void executePostMethod() throws IOException,InterruptedException {
        Map<String,String> responseMap = new HashMap<>();
        String data = "{\"id\":\"XYZ123\",\"name\":\"John Doe\",\"accountNumber\":\"ABC123\"}";
        URL urlObject = new URL("\"http://localhost/v1/name/validate\"");
        System.out.println("Creating Connection");
        Socket socket = new Socket(InetAddress.getByName(urlObject.getHost()),7080);
        System.out.println("Connection Established");

        PrintWriter printWriter = new PrintWriter(socket.getoutputStream());
        printWriter.println("POST " + urlObject.getFile() + " HTTP/1.0");
        printWriter.println("Host: " + urlObject.getHost());
        printWriter.println("Content-Length: " + data.length());
        printWriter.println("Content-Type: " + "application/json");
        printWriter.println();   //Writing an empty line just to notify the server the header ends here
        // and next thing written will the data/content
        printWriter.println(data);
        printWriter.println();
        printWriter.flush();
        Thread.sleep(5000);
        socket.setSoLinger(true,0);
        socket.close();
    }

在服务器发送响应之前,关闭套接字和行 socket.setSoLinger(true,0); 我相信,发送 RST 数据包。但我得到了 org.apache.catalina.connector.ClientAbortException: java.io.IOException: broken pipe 错误而不是 Connection reset by peer

如何重现java.io.IOException: Connection reset by peer

更新

我分析了 TCP 连接,这就是我得到的。服务器在尝试向客户端发送数据之前收到 RST 数据包

enter image description here

现在据我所知,如果客户端向服务器发送 RST 数据包,服务器得到 Connection reset by peer ,我只得到 broken pipe

解决方法

这可能对任何试图找出错误及其原因的人有所帮助。

上述程序确实有效,但我发现它依赖于服务器。我在不同的服务器上尝试了上面的代码,我得到了

Caused by: java.io.IOException: Connection reset by peer

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...