On 9/8/22 00:25, Richard Hodges via Boost wrote:
How do I get that information without sending any read/write request?
You don't. This is like being blind and asking, "how do I know that the person is still there if I don't listen or reach out to touch him?"
Keep-alive *is* that implicit sending request, that's the whole point of it. I didn't test this but my understanding is that when the connection is closed according to TCP keep-alive settings then: 1. If you're using the connection socket with select/poll/epoll/etc. then the socket becomes signalled with error (as if closed). 2. Otherwise, if you're not blocking signals, SIGPIPE is delivered. 3. Otherwise, you will find that the connection is closed the next time you try to send. So, unless you have detected the connection is closed by one of the above methods, you can assume it is *probably* alive. I say "probably" because it may not be actually alive - only that the keep-alive mechanism has not yet detected its termination. Keep-alive settings are very (too) liberal on most systems, to the point that the mechanism is not practical in most cases when you actually need it to detect connection loss. Which is why often applications don't bother with it and implement their own pinging protocols.