TCP Socket 无连接超时 [英] TCP Socket no connection timeout

查看:29
本文介绍了TCP Socket 无连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开一个 TCP 套接字并将其连接到网络上其他地方的另一个套接字.然后我可以成功发送和接收数据.我有一个计时器,每秒向套接字发送一些内容.

I open a TCP socket and connect it to another socket somewhere else on the network. I can then successfully send and receive data. I have a timer that sends something to the socket every second.

然后我通过强行断开连接(在这种情况下拔出以太网电缆)来粗鲁地中断连接.我的套接字仍然报告它每秒都在成功地写出数据.这会持续大约 1 小时 30 分钟,最终会出现写入错误.

I then rudely interrupt the connection by forcibly losing the connection (pulling out the Ethernet cable in this case). My socket is still reporting that it is successfully writing data out every second. This continues for approximately 1hour and 30 minutes, where a write error is eventually given.

什么指定了套接字最终接受另一端消失的超时?是操作系统(Ubuntu 11.04),是来自 TCP/IP 规范,还是套接字配置选项?

What specifies this time-out where a socket finally accepts the other end has disappeared? Is it the OS (Ubuntu 11.04), is it from the TCP/IP specification, or is it a socket configuration option?

推荐答案

拔网线不会中断 TCP 连接(1),但会中断通信.您可以重新插入电缆,一旦建立 IP 连接,所有后台数据都将移动.这就是 TCP 可靠的原因,即使在蜂窝网络上也是如此.

Pulling the network cable will not break a TCP connection(1) though it will disrupt communications. You can plug the cable back in and once IP connectivity is established, all back-data will move. This is what makes TCP reliable, even on cellular networks.

当 TCP 发送数据时,它期待一个 ACK​​ 响应.如果在一段时间内没有数据,它会重新传输数据并再次等待.它在传输之间等待的时间通常呈指数增长.

When TCP sends data, it expects an ACK in reply. If none comes within some amount of time, it re-transmits the data and waits again. The time it waits between transmissions generally increases exponentially.

经过一定次数的重传或一定的总时间后没有 ACK,TCP 将认为连接断开".多少次或多长时间取决于您的操作系统及其配置,但它通常会超时几分钟.

After some number of retransmissions or some amount of total time with no ACK, TCP will consider the connection "broken". How many times or how long depends on your OS and its configuration but it typically times-out on the order of many minutes.

来自 Linux 的 tcp.7 手册页:

From Linux's tcp.7 man page:

   tcp_retries2 (integer; default: 15; since Linux 2.2)
          The maximum number of times a TCP packet is retransmitted in
          established state before giving up.  The default value is 15, which
          corresponds to a duration of approximately between 13 to 30 minutes,
          depending on the retransmission timeout.  The RFC 1122 specified
          minimum limit of 100 seconds is typically deemed too short.

这可能是您要调整的值,以更改检测连接是否消失所需的时间.

This is likely the value you'll want to adjust to change how long it takes to detect if your connection has vanished.

(1) 也有例外.操作系统在注意到电缆被移除后,可以通知上层所有连接都应被视为断开".

这篇关于TCP Socket 无连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆