connect reset 与connect reset by peer 原因分析

     在项目组经常遇到TCP连接的问题,一直是遇到了网上查资料,没有自己总结,感觉对这两种错都是模模糊糊,正好,今天又有个现场遇到此种问题需要我帮忙分析解决。通过查阅各种资料,算是比之前理解更加深刻。
     connect reset:原因是因客户方已经把TCP连接断开了,服务方还在往socket中写数据。要重现connect reset  需要满足条件:客户端代码中代码要增加socket.setSoLinger(true, 0);查看java api,此句只对调用socket.close()有用,其作用就是当客户端调用socket.close()会像服务端发送reset信号,如果服务端再对此连接进行读、写操作时,服务端就会报connection reset的异常。
connect reset by peer :此异常常见与服务端处理异常退出时,客户端还在往socket里面写数据。前提条件也是:服务端代码也设置了socket.setSoLinger(true, 0);
下面是验证代码

客户端代码:
package com.test.client;

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Client {
private static int port = 9999;
private static String host = “127.0.0.1”;
public static SimpleDateFormat sdf = new SimpleDateFormat(“yy-MM-dd HH:mm:ss.SSS”);

public static void main(String[] args) throws Exception {
    Socket socket = new Socket();
    socket.setSoLinger(true, 0);

// CASE 3 ?/ socket.setSoLinger(true, 1);
SocketAddress address = new InetSocketAddress(Client.host, Client.port);
socket.connect(address);
OutputStream output = socket.getOutputStream();
StringBuilder strB = new StringBuilder();
for (int i = 0;
i < 10;
i++) {
strB.append(“H”);
}
byte[] request = strB.toString().getBytes(“utf-8”);
System.out.println("Client before write : " + sdf.format(new Date()));
output.write(request);
System.out.println("Client after write : " + sdf.format(new Date()));
socket.close();
System.out.println("Client after close : " + sdf.format(new Date()));
}
}

服务端代码:
package com.test.server;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Server {
private static int queueSize = 10;
private static int port = 9999;
public static SimpleDateFormat sdf = new SimpleDateFormat(“yy-MM-dd HH:mm:ss.SSS”);

public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket();
    serverSocket.setReuseAddress(true);
    serverSocket.setReceiveBufferSize(128 * 1024);
    serverSocket.bind(new InetSocketAddress(Server.port), Server.queueSize);
    Socket socket = null;
    while (true) {
        socket = serverSocket.accept();

// socket.setSoLinger(true,0);
InputStream input = socket.getInputStream();
while(input.read() !=-1){
throw new Exception();
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
Thread.sleep(10 * 1000);
System.out.println("Server before read : " + sdf.format(new Date()));
while ((length = input.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
System.out.println("Server after read : " + sdf.format(new Date()));
String req = new String(output.toByteArray(), “utf-8”);
System.out.println(req.length());
socket.close();
}
}
}
经过此次实际验证,详细对此两个异常有更深刻的理解。后续遇到类似问题,思路会清晰很多。

你可能感兴趣的:(java)