How long does a winsock connection take?

I have a C ++ program using winsock that connects to a server, the user will need to periodically send data to that server for a very long period of time (maybe a week without having to reconnect).

I've found a lot of documentation on timeouts when establishing a connection, but I'm trying to figure out how long a connection lasts after it has been established. Does the connection continue until the program is disconnected? Can I connect, wait two hours to send anything?


source to share

1 answer

There is no explicit limit to the lifetime of a connection (at least in TCP). The connection lasts until the following:

  • Endpoint (application) disconnects (in fact, the connection may remain in half duplex)
  • The intermediate entity decides to terminate the connection (eg firewall, NAT, etc.).

In the "real world", Internet connections are usually forced to close after a certain period of time, especially if there is no data. Also, depending on the protocol, some servers refuse to keep the connection open indefinitely (for example, http servers).

In conclusion: there is no general way to know the lifetime of a connection. You are completely in the hands of firewalls, proxies (if applicable) and on behalf of the server.

Usually passing some data (like keep-alive messages) helps. It also helps detect that the connection has been silently dropped.



All Articles