TCP 连接寿命 [英] TCP Connection Life

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

问题描述

客户端/服务器 TCP 连接可以持续多久?

How long can I expect a client/server TCP connection to last in the wild?

我希望它保持永久连接,但事情发生了,所以客户端必须重新连接.我在什么时候说代码有问题而不是某些外部设备有问题?

I want it to stay permanently connected, but things happen, so the client will have to reconnect. At what point do I say that there's a problem in the code rather than there's a problem with some external equipment?

推荐答案

我同意 Zan Lynx 的观点.无法保证,但假设没有连接或带宽问题,您可以通过在连接上发送数据来几乎无限期地保持连接有效.

I agree with Zan Lynx. There's no guarantee, but you can keep a connection alive almost indefinitely by sending data over it, assuming there are no connectivity or bandwidth issues.

通常我已经采用了应用程序级别的保持活动方法,尽管这通常是因为它已经在客户端规范中,所以我不得不这样做.但只需每两分钟发送一些简短的数据,您希望得到某种确认.

Generally I've gone for the application level keep-alive approach, although this has usually because it's been in the client spec so I've had to do it. But just send some short piece of data every minute or two, to which you expect some sort of acknowledgement.

是否将一次失败确认为连接失败取决于您.一般来说,这就是我过去所做的,尽管有一种情况我已经连续等待三个失败的响应来断开连接,因为连接另一端的应用程序在响应你在那里时非常不稳定"?"请求.

Whether you count one failure to acknowledge as the connection having failed is up to you. Generally this is what I have done in the past, although there was a case I had wait for three failed responses in a row to drop the connection because the app at the other end of the connection was extremely flaky about responding to "are you there?" requests.

如果连接失败,在某些时候它可能会失败,即使机器在同一网络上,然后尝试重新建立它.如果失败了设定的次数,那么你就有问题了.如果您的连接在连接一段时间后仍然失败,那么您就有问题了.在这两种情况下,很可能是网络问题,而不是您的代码,或者您机器上的 TCP/IP 堆栈有问题(众所周知:我在旧版本的 QNX 上遇到了这个问题——它会只是随机摔倒).话虽如此,您可能遇到了软件问题,而确定知道的唯一方法通常是附加调试器,或者在那里进行一些登录.例如.如果您始终可以成功连接,但一段时间后您不再收到 ACK,即使在重新连接后,也可能是您的服务器出现死锁,或陷入循环或其他问题.

If the connection fails, which at some point it probably will, even with machines on the same network, then just try to reestablish it. If that fails a set number of times then you have a problem. If your connection persistently fails after it's been connected for a while then again, you have a problem. Most likely in both cases it's probably some network issue, rather than your code, or maybe a problem with the TCP/IP stack on your machine (has been known: I encountered issues with this on an old version of QNX--it'd just randomly fall over). Having said that you might have a software problem, and the only way to know for sure is often to attach a debugger, or to get some logging in there. E.g. if you can always connect successfully, but after a time you stop getting ACKs, even after reconnect, then maybe your server is deadlocking, or getting stuck in a loop or something.

真正有用的是在各种负载条件下设置一系列长时间运行的测试,从仅仅发送 keep alive are you there?/ack 请求和响应,到绝对打击服务器.这通常会让您对您的软件组件更有信心,并且对于解决一些非常奇怪的问题非常有用,这些问题不一定会导致您的连接出现问题,尽管它们可能会导致发生的事务出现问题.例如,我曾经写过一个电信应用服务器,提供号码翻译等服务,我们只是让它一次运行几天.问题是当星期六到来时,一整天,它都会拒绝进来的每个呼叫请求,这相当于数百万个呼叫,我们不知道为什么.原来是因为某些日期转换代码中的一个错字导致周六出现问题.

What's really useful is to set up a series of long-running tests under a variety of load conditions, from just sending the keep alive are you there?/ack requests and responses, to absolutely battering the server. This will generally give you more confidence about your software components, and can be really useful in shaking out some really weird problems which won't necessarily cause a problem with your connection, although they might result in problems with the transactions taking place. For example, I was once writing a telecoms application server that provided services such as number translation, and we'd just leave it running for days at a time. The thing was that when Saturday came round, for the whole day, it would reject every call request that came in, which amounted to millions of calls, and we had no idea why. It turned out to be because of a single typo in some date conversion code that only caused a problem on Saturdays.

希望有所帮助.

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

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