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

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

问题描述

我打开一个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.

什么规定了套接字最终接受另一端的消失的超时时间?是OS(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手册页:

   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套接字无连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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