保持活动的TCP / IP连接的套接字在互联网上 - 什么时候?怎么样?多少钱? [英] Keep Alive TCP/IP connected sockets over the Internet - when? how? and how much?

查看:152
本文介绍了保持活动的TCP / IP连接的套接字在互联网上 - 什么时候?怎么样?多少钱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:如果您想保存自己漫长的preamble跳到答案

UPDATE: Skip to the answer if you want to save yourself the lengthy preamble.

TCP / IP连接保持连接被指定为至少每两小时一次:的http://tool​​s.ietf .ORG / HTML / RFC1122#页面101 。问题是这样写于1989年,关心发送额外的keepalive报文的成本!然而,它仍然是默认的时间大多数操作系统会按照规范发送保活向下连接插座闲置一段时间后!当然,时下最连接(尽管那些设置,如果不活动,如果连接到对等通过互联网连接之前死在您不知情的方式方法之前超时超时高于 - 我怀疑,因为两者之间的路由器表不打扰保持它活着 - 我一直想知道在哪里泄漏的最后一条消息去... 更新:这样做的原因是路由器,可能是在你或远程主机到底是有状态和连接感知和降不活动的一段时间后,连接 - 你的经历在互联网上无法删除您的连接路由器 - 他们,他们不关心 - 数据包只发送它已去)。所以,我已经看到了2个普通解决方案,以保持那些活着的连接在互联网上:

TCP/IP connections KeepAlives are specified to be at least once every two hours: http://tools.ietf.org/html/rfc1122#page-101. The problem is this was written in 1989 and is concerned about the cost of sending the extra KeepAlive packet! Yet it still is the default time most OS's in accordance with the spec send out KeepAlives down a connected socket after a period of inactivity! Of course nowadays most connections timeout way before that if inactive, and if connected to a peer over the Internet connections die without your knowledge way before that (despite setting ones timeout higher than that - I suspect because router tables in between don't bother keeping it alive - I always wondered where the leaked last message goes... UPDATE: The reason for this is 'routers' that may be at your or the remote hosts end are 'stateful' and connection aware and drop the connection after some period of time of inactivity - the routers that your go through over the Internet cannot drop your connection - they they dont care - the packet is just send where it has to go). So I have seen 2 common solutions to keeping ones connection alive over the Internet:

1),否认(修改如已经向我指出这不是不顾规范它只是改变了默认设置)的规范,改变你的全系统的存活时间间隔,以更低的2个多小时,或2)同行定期实现自己'的KeepAlive系统投票。

1) Disregard ( as has been pointed out to me this is not disregarding the spec it is just changing the default) the spec and change your system wide KeepAlive interval to lower than 2 hours, or 2) implement your own 'KeepAlive' system polling the peer periodically.

无论哪种方式;是什么(不活动在此发送您的KeepAlive)一个合适的时期?我已经看到了从1秒到默认2小时。看来号被吸出大拇指...如果我有一个客户端应用程序可能随时随地连接世界上什么是安全的和合理的时间(我想要一个单一的持久连接)?连接到一个对等很多跳,走在世界的通过互联网的连接301秒死亡的另一边(虽然你只知道当你尝试送东西),所以时间设置为300秒似乎是一个神奇的数字 - 我得到的KeepAlive1秒去世前 - 这间隔从未失败过我。但它是安全的。

Either way; what is a suitable period (of inactivity at which to send your KeepAlive)? I have seen everything from 1 second to the default 2 hours. It seems the number is sucked out of thumbs... If I have a client application connecting from potentially anywhere in the world what is safe and reasonable period (I want a single persistent connection)? Connecting to a peer many hops away on the other side of the world over the Internet the connection dies on 301 seconds (though you only know about it when you try send something) so setting the period to 300 seconds seems to be the magic number - I get the KeepAlive 1 second before death - this interval has never failed me.. but is it safe?

编辑::此特定连接IM实现在C#3.0因此code。在欢迎

This particular connection im implementing in C# 3.0 so code in that welcome.

推荐答案

当你想prevent你的连接被丢弃。

When you want to prevent you connection being dropped.

如何

通过以下方法发送TCP / IP保持连接闲置一段时间可能后,送点东西或发送自己的东西(因为设置在该TCP / IP持久连接发送将应用在系统级对所有连接的插座,而不是在此期限reccomended应用程序级别)。

By sending something after a period of inactivity possible by either sending TCP/IP KeepAlives or sending something yourself (reccomended since setting the period at which TCP/IP KeepAlives are sent will apply at the system level to all connected sockets instead of at the application level).

多少

有多长的绳子?首先,你必须明白,为什么连接断开:

How long is a piece of string? First you have to understand why the connection is dropped:

连接中断的原因是:

家用/商用路由器和防火墙这是状态和连接感知往往闲置一段时间后放弃外部TCP连接

所以它拥有的没有的与你的应用程序或TCP / IP本身,而是用的硬件或软件做的您的连接经过。你可以做一些研究活动在此家用/商用设备/软件,您可以通过断开连接(参见下面的列表)的典型周期。但是,如果对等网络,在互联网上潜在的任何用户:

So it has nothing to do with your application or TCP/IP per se, but to do with the hardware or software your connection goes through. You could do some research into typical periods of inactivity at which home/commercial devices/software you may go through drop a connection (see lists below). However if the peer could be potentially any user on the Internet:

最后,如果你不知道什么样的设备/ sofware他们所经历:允许客户自行设置的非活动期(在该送点东西来保持连接)

发送在很短的时间间隔保持连接(或同等学历)(闲置),以适应所有情况下(虽然不必要的流量是一件坏事额外的数据包闲置每隔几秒钟时下正处于下降海洋,除了移动网络仍然)。但要注意按上TCP规范/ IP它应该是生存的临时故障所以设置太低可能无法存活暂时停运,从的 http://aplawrence.com/Bofcusm/2426.html

Or send KeepAlives (or your equivalent) at short intervals (of inactivity) to accommodate all cases (though unnecessary traffic is a bad thing an extra packet every few seconds of inactivity nowadays is a drop in the ocean, except mobile networks still). But be warned as per the specification on TCP/IP it is supposed to survive temporary outages so setting it too low could have the undesirable affect of not surviving a temporary outage, from http://aplawrence.com/Bofcusm/2426.html:

紧缩的存活时间参数的一面是,你也
  严格限制在电缆中断的脸TCP的应变能力。会话
  其两端是活的,可以运行正常保持活着,即使
  中间电缆或路由器断开了几分钟。如果
  你告诉服务器发送疯狂保活,就会发现一个
  原本生存电缆断和断开会话
  好听。

The down side of tightening the keepalive parameters is that you also sharply limit TCP's resilience in the face of cable outages. A session whose both ends are alive and ready to run normally stays alive even if an intermediate cable or router is disconnected for a few minutes. If you've told the server to frantically send keepalives, it will notice a cable break and disconnect sessions that would otherwise have survived nicely.

不过,所以你必须要处理这个,我认为这种担忧是过时,因为一个人不得到暂时中断(电缆被拔出,他们更可能是永久性的),你将有一个中断,如果你是无论如何做一些通讯科永久连接(而不是狡猾拨号说)。如果你有一个中断,你可能要重新建立连接(如RDP一样)否(如Telnet)。

But you would have an outage if your was doing some comms anyway so you have to handle this and I think this concern is outdated as one does not get temporary outages (they are more likely to be permanent - cable unplugged) with todays persistent connections (as opposed to dodgy dial-up say). Should you have an outage you may want to re-establish your connection (such as RDP does) or not (such as Telnet).

一些研究成什么常见的应用是:

Some research into what common applications use:

App    |    KeepAlive sent after    |    configurable    | ref
--------------------------------------------------------------
Telnet |    600 seconds             |          Y         | http://www-01.ibm.com/support/docview.wss?uid=nas14adccb91a24d9ed28625694900513857
MS RDP |    ?

和一些研究,什么设备/软件降不活动的连接时:

And some research into what devices/software drop inactive connections and when:

Device/SW                |    dropped after    |    configurable    | ref
--------------------------------------------------------------
Windows 2003 Firewall    |    24 hours         |          ?         | http://technet.microsoft.com/en-us/library/cc755604(WS.10).aspx
SonicWall TZ100/200      |     5 mins          |          Y         |  http://www.sonicwall.com/downloads/SonicWALL_TZ_100_200_Series_Getting_Started_Guide.pdf
Netgear FR114P           |     5 mins          |          N         | http://www.broadbandreports.com/forum/remark,4182300
Cisco ASA                |     1 hour          |          Y         | http://www.cisco.com/c/en/us/td/docs/security/asa/asa91/configuration/firewall/asa_91_firewall_config/conns_connlimits.html

请编辑到/细化这些列表为所有人的利益。

Please edit to/refine these lists for the benefit of all.

这篇关于保持活动的TCP / IP连接的套接字在互联网上 - 什么时候?怎么样?多少钱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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