From what I know, blocking reception on a TCP socket does not always detect a connection error (due to a network failure or a failure of the remote endpoint), returning a value -1or raising an IO exception: sometimes it can just hang indefinitely.
One way to solve this problem is to set a timeout to block. In case the upper limit of the reception time is known, this limit can be set as a timeout, and the connection can be considered lost simply after the timeout; when such an upper bound is unknown a priori, for example, in a pub-sub system where the connection remains open for receiving publications, the timeout that should be set will be somewhat arbitrary, but its expiration may cause a ping / pong request to verify that the connection (and the endpoint too) is still.
I wonder if using asynchronous reception manages the problem of detecting a connection failure. In boost :: asio, I would call the socket::asynch_read_some()registration of the handler for an asynchronous call, whereas in java.nio I would configure the channel as non-blocking and register it for the selector with the flag of interest OP_READ. I assume that the correct detection of a connection failure would mean that in the first case the handler would be called with an error code other than 0, while in the second case the selector selects the faulty channel, but the next one read()on the channel will either return -1or throw IOException.
Is this behavior guaranteed by asynchronous reception, or can there be scenarios where, after a connection failure, for example, in boost :: asio, the handler will never be called or in java.nio the selector will never select the channel?
.