When you send data, your blocking call will return when data is written to the TCP output buffer. It only blocks the filling of the buffer, waiting for the server to confirm receipt of previously sent data.
As soon as the data is in the buffer, the network drivers will try to send the data. If the connection is lost, in the second recording attempt, your application detects a failed connection status.
Also, how does the connection close? Is the server actively closing the connection? In this case, the client socket will be notified the next time the socket is called. Or did he crash? Or maybe there is a network error, which means that you can no longer communicate.
Faulty connection detection occurs only when trying to send or receive data via a socket. This is different from the fact that the connection is actively closed. You simply cannot determine if the connection is alive without doing something with it.
So, try to make sock.recv(0) after recording - if the socket fails, it will raise " Errno::ECONNRESET: Connection reset by peer - recvfrom(2) ". You can also try sock.sendmsg "", 0 (not sock.write, or sock.send) and this will Errno::EPIPE: Broken pipe - sendmsg(2) " Errno::EPIPE: Broken pipe - sendmsg(2) ".
Even if you have hands on TCP packets and receive confirmation that the data was received from the other end, there is still no guarantee that the server will process this data - it can be in its input buffer but not yet processed.
All this can help identify a damaged connection earlier, but it still does not guarantee that the data was received and processed by the server. The only sure way to know that the application has processed your message is through an application-level response.
source share