Follow Excellent, Success will Chase you

0%

Socket recv —— Connection reset by peer (104)

1
n = recv(socket_fd, buf, len, 0);

在接收数据时:

有时recv的返回值n<0,错误码:104(Connection reset by peer),为啥?

有时recv的返回值n=0,对端socket关闭,如果对端socket没有关闭,为啥n=0

Connection reset by peer : (ECONNRESET 104)

socket read/recv过程中,对方socket中断,read/recv会先返回已经发送的字节数,再次write时返回-1,errno号为ECONNRESET(104).即:read/recv 一个已收到RST的socket,系统会发SIGPIPE信号给该进程,如果将这个信号交给系统处理或者直接忽略掉了,read/recv都返回EPIPE错误.因此对于socket通信一定要捕获此信号,进行适当处理 ,否则程序的异常退出将会给你带来灾难。

The client’s call to readline may happen before the server’s RST is received by the client, or it may happen after.
If the readline happens before the RST is received, as we’ve shown in our example, the result is an unexpected EOF in the client.
But if the RST arrives first, the result is an ECONNRESET ("Connection reset by peer") error return from readline.
What happens if the client ignores the error return from readline and writes more data to the server?
This can happen, for example, if the client needs to perform two writes to the server before reading anything back, with the first write eliciting the RST.
The rule that applies is: When a process writes to a socket that has received an RST, the SIGPIPE signal is sent to the process.
The default action of this signal is to terminate the process, so the process must catch the signal to avoid being involuntarily terminated.
If the process either catches the signal and returns from the signal handler, or ignores the signal, the write operation returns EPIPE.

tcp_syncookies

在高并发的情况下,内核会认为系统受到了SYN flood攻击,会发送cookies(possible SYN flooding on port 80. Sending cookies),这样会减慢影响请求的速度,所以在应用服务武器上设置下这个参数为0禁用系统保护就可以进行大并发测试了。

1
net.ipv4.tcp_syncookies = 0

返回值: n==0

TCP_NODELAY

参考

-------------本文结束感谢您的阅读-------------
  • 本文作者: Winddoing
  • 本文链接: https://winddoing.github.io/post/33577.html
  • 作者声明: 本博文为个人笔记, 由于个人能力有限,难免出现错误,欢迎大家批评指正。
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!