Java 如何通过对等机复制连接重置?
我有一个服务器,它在日志中打印了以下内容Java 如何通过对等机复制连接重置?,java,spring-boot,connection-reset,Java,Spring Boot,Connection Reset,我有一个服务器,它在日志中打印了以下内容 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.writeFrom
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”,服务器日志将通过对等方重置连接
为了复制它,我让服务器休眠了几秒钟,还创建了一个客户端:
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();
}
private void executePostMethod()引发IOException、InterruptedException{
Map responseMap=newhashmap();
字符串数据=“{\“id\”:\“XYZ123\”,\“name\”:“John Doe\”,“accountNumber\”:“ABC123\”;
URL URL对象=新URL(“\”http://localhost/v1/name/validate\"");
System.out.println(“创建连接”);
套接字=新套接字(InetAddress.getByName(urlObject.getHost()),7080);
System.out.println(“已建立连接”);
PrintWriter PrintWriter=新的PrintWriter(socket.getOutputStream());
printWriter.println(“POST”+urlObject.getFile()+“HTTP/1.0”);
printWriter.println(“主机:+urlObject.getHost());
printWriter.println(“内容长度:+data.Length());
printWriter.println(“内容类型:“+”应用程序/json”);
printWriter.println();//写一个空行只是为了通知服务器头到此结束
//接下来要写的是数据/内容
printWriter.println(数据);
printWriter.println();
printWriter.flush();
睡眠(5000);
套头固定螺栓(正确,0);
socket.close();
}
在服务器发送响应之前,关闭套接字和行socket.setSoLinger(true,0)代码>我相信,发送RST数据包。但是我得到了org.apache.catalina.connector.ClientAbortException:java.io.IOException:breaked pipe
错误,而不是由对等方重置连接
如何重现java.io.IOException:对等方重置连接
更新
我分析了TCP连接,这就是我得到的。服务器在尝试向客户端发送数据之前接收RST数据包
现在据我所知,如果客户端向服务器发送RST数据包,服务器会被对等方重置连接,我得到的所有信息都是管道破裂,这可能对任何试图找出错误及其原因的人都有帮助 上面提到的程序确实有效,但我发现它依赖于服务器。我在另一台服务器上尝试了上述代码,结果
Caused by: java.io.IOException: Connection reset by peer
尝试在打开套接字后设置SO_LINGER,并删除睡眠。我也这样做了,我将SO_LINGER行保持在这一行下面,socket socket=new socket(InetAddress.getByName(urlObject.getHost()),7080);并且去掉了线。睡眠(5000)线也一样,但我还是得到了代理管道。或者您是指来自服务器的睡眠?
管道破裂
和对等方连接重置
是针对相同条件的两条消息。你的追求已经成功了。