我在C ++中使用连接到服务器的winsock程序,用户需要在很长一段时间内定期向该服务器发送数据(可能需要几周而不需要重新连接)。
我在建立连接时找到了大量关于超时的文档,但我试图找出连接建立后连接的持续时间。 连接是否一直持续到任一程序关闭? 我可以连接然后等两个小时发送一些东西吗?
I have a program in C++ using winsock that connects to a server, the user's will need to send data to this server periodically over a very long span of time(perhaps weeks without the need to reconnect).
I have found plenty of documentation on timeouts when establishing a connection, but I am trying to find out how long the connection lasts after it has been established. Does the connection last until either program is shut down? Can I connect then wait two hours to send something?
最满意答案
没有明确的连接生命周期限制(至少在TCP中)。 连接持续到以下之一:
端点(应用程序)关闭(实际上连接可能保持半双工模式) 中间实体决定终止连接(例如防火墙,NAT等)在“真实世界”中,互联网连接通常会在一段时间后强行关闭,尤其是在没有数据发送的情况下。 除此之外,根据协议,一些服务器拒绝保持连接无限期打开(例如http服务器)。
总之:没有通用的方法来发现连接的生命周期。 您完全掌握在防火墙,代理(如果适用)和服务器的手中。
定期发送一些数据(例如保持活动消息)通常会有所帮助。 它还有助于检测连接是否已静默终止。
There's no explicit connection lifetime limitation (at least in TCP). The connection lasts until one of the following:
Either endpoint (application) shuts down (actually the connection may remain in half-duplex mode) Intermediate entity decides to terminate the connection (such as firewall, NAT or etc.)In "real-world" internet connections are usually shut down forcibly after some period of time, especially if there's no data sent. Besides of this, depending on the protocol, some servers refuse to keep the connection open for indefinite time (such as http servers).
In conclusion: there's no generic way to discover the lifetime of the connection. You're completely in the hands of the firewalls, proxies (if applicable), and the server behalf.
Sending some data periodically (such as keep-alive messages) usually help. It also helps to detect that the connection has been silently terminated.
更多推荐
发布评论