winsock连接有多长时间了?(How long does a winsock connection last?)

编程入门 行业动态 更新时间:2024-10-26 08:22:49
winsock连接有多长时间了?(How long does a winsock connection last?)

我在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.

更多推荐

本文发布于:2023-07-30 22:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长时间   有多   winsock   long   connection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!