문제

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:

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

  • How can I check if connection is alive or dead without actually using the send and recv? If I have to use send and recv functions then what's the point of using keep-alive in the first place?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top