cpx
cpx

Reputation: 17577

When is Keep-alive required for TCP Sockets?

As far as I know Keep-alive on a TCP socket is helpful to know if the sockets aren't just opened and a connection is actually alive between the two sockets. So, I have a couple of questions I'd like to inquire regarding the usage of Keepalive in Winsocks2:

Upvotes: 1

Views: 1782

Answers (2)

user207421
user207421

Reputation: 310980

What happens when keep-alive option detects a dead socket?

The connection is reset, and any reads or writes get a 'connection reset' error. Note that keepalive is off by default, and when enabled only operates at two-hour intervals by default.

How can I check if connection is alive or dead without actually using the send and recv?

You can't. TCP/IP is deliberately designed not to have a 'dial tone'. It works much better that way. This is a major reason why it has displaced all the prior protocols such as SNA that did.

If I have to use send and recv functions then what's the point of using keep-alive in the first place?

recv() won't tell you about a broken connection. It may just block forever. You can use read timeouts, but then you have to decide how much time is too much. Or, you can implement an application-level PING.

Upvotes: 2

Tim B
Tim B

Reputation: 41208

Keep alive detects if the server at the other end of the connection (or a physical link such as a network being down) has died before you send a message. Otherwise the disconnection is only detected when you actually try to send data, which if your connection is idle for some reason could take a long time.

Upvotes: 1

Related Questions