I'm playing around with Python and listening for UDP packets on a given port, everything seems to be working nicely - but after an extended period of time the script crashes with the following error:
data = self._sock.recv(self._rbufsize)
socket.error: [Errno 54] Connection reset by peer
When restarting just the script, the same crash occurs again after a shorter period of time. Restarting the server instead seems to resolve the problem completely for a while again.
With respect to the socket side of things, I'm doing:
UDP_IP = "0.0.0.0"
UDP_PORT = 6000
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind( (UDP_IP, UDP_PORT) )
Am I missing something obvious or is there just a simple way of avoiding this?
Thanks in advance for any light you can shed!
Benji
The error "Connection reset by peer" on a UDP socket, means the client has received an ICMP error message (for example: port unreachable, TTL exceeded, etc.) from the server on a packet it has sent.
I can't say for sure what is causing this in your code, but I can offer two ideas:
SO_REUSEADDR
is causing the problem. This socket option lets you open multiple sockets on the same port. What possibly happens, is that some other process is trying to use port 6000, succeeds (because you tell the OS not to block it), and your socket is closed by the OS. Since I don't see a reason to use SO_REUSEADDR
on UDP sockets, I would suggest you remove the line sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
and try again.Best of luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With