I’m using the following TCP communication code(server side) to handle the timeout etc…
If I kill the client side software than an exception happens(socket.error), but when I unplug the ethernet cable from client PC than only the socket.timeout exception happens, so how can I know that the connection is broken? Is that possible that I will get a socket.error exception after some minutes? I tried with 3minutes but I haven’t got the socket.error exception.
self.request.settimeout( 1 )
while 1:
try:
response = self.request.recv( buffSize )
if(response)
...response handler....
else:
...disconnect handler....
except socket.timeout :
...timeout handler...
except socket.error:
...disconnect handler...
It depends on how quickly you want to detect the broken connection. If you are willing to wait a few minutes, you can turn on TCP Keepalive (https://www.rfc-editor.org/rfc/rfc1122#page-101):
Note, the actual time interval used by TCP is operating system dependent.
If you need something faster than what TCP Keepalive offers, you probably need to introduce a "ping" feature to your protocol. What this means is your client and server periodically exchange a message that is a null operation. It just tests that the connection is alive.