Python Tcp disconnect detection
python, tcp
Solution
You'll get a `socket.error`:`[Errno 104] Connection reset by peer` exception (aka `ECONNRESET`) on any call to `send()` or `recv()` if the connection has been lost or disconnected. So to detect that, just catch that exception:
while True:
try:
s.send(bytes('hello', 'UTF-8'))
except socket.error, e:
if e.errno == errno.ECONNRESET:
# Handle disconnection -- close & reopen socket etc.
else:
# Other error, re-raise
raise
time.sleep(1)
Problem
I have a simpletcp example: ``` import socket import time TCP_IP = '127.0.0.1' TCP_PORT = 81 BUFFER_SIZE = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TCP_IP, TCP_PORT)) while True: s.send(bytes('hello', 'UTF-8')) time.sleep(1) s.close() ``` How can I detect, if I lost the connection to the server, and how can I safely reconnect then? Is it necessary to wait for answer to the server? UPDATE: ``` import socket import time TCP_IP = '127.0.0.1' TCP_PORT = 81 BUFFER_SIZE = 1024 def reconnect(): toBreak = False while True: s.close() try: s.connect((TCP_IP, TCP_PORT)) toBreak = True except: print ("except") if toBreak: break time.sleep(1) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((TCP_IP, TCP_PORT)) while True: try: s.send(bytes('hello', 'UTF-8')) print ("sent hello") except socket.error as e: reconnect() time.sleep(1) s.close() ``` If I break the connection, it raises an error (does not really matter what), and goes to the reconnect loop. But after I restore the connection, the connect gives back this error: OSError: [WinError 10038] An operation was attempted on something that is not a socket If I restart the script, which calls the same s.connect((TCP_IP, TCP_PORT)), it works fine.