Note that there are some explanatory texts on larger screens.

plurals
  1. POPython socket error on UDP data receive. (10054)
    primarykey
    data
    text
    <p>I currently have a problem using UDP and Python socket module. We have a server and clients. The problem occurs when we send data to a user. It's possible that user may have closed their connection to the server through a client crash, disconnect by ISP, or some other improper method. As such, it is possible to send data to a closed socket.</p> <p>Of course with UDP you can't tell if the data really reached or if it's closed, as it doesn't care (atleast, it doesn't bring up an exception). However, if you send data and it is closed off, you get data back somehow (???), which ends up giving you a socket error on sock.recvfrom. [Errno 10054] An existing connection was forcibly closed by the remote host. Almost seems like an automatic response from the connection.</p> <p>Although this is fine, and can be handled by a try: except: block (even if it lowers performance of the server a little bit). The problem is, I can't tell who this is coming from or what socket is closed. Is there anyway to find out 'who' (ip, socket #) sent this? It would be great as I could instantly just disconnect them and remove them from the data. Any suggestions? Thanks.</p> <p>Server:</p> <pre><code>import socket class Server(object): def __init__(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.connected = {} def connect(self): self.socket.bind(('127.0.0.1', 5579)) def find_data(self): while 1: data, address = self.socket.recvfrom(1024) self.got_data(data,address) if self.connected.has_key(address): pass else: self.connected[address] = None def got_data(self, data, address): print "GOT",data,"FROM",address for people in self.connected: print people self.send_data('hi', people) def send_data(self, data, address): self.socket.sendto(data,address) if __name__ == '__main__': server = Server() server.connect() print "NOW SEARCHING FOR DATA" server.find_data() </code></pre> <p>Client:</p> <pre><code>import socket, time class Client(object): def __init__(self): self.socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) def connect(self): self.socket.connect(('127.0.0.1', 5579)) def send_data(self): self.socket.sendto('hi',('127.0.0.1', 5579)) def got_data(self, data, address): print "GOT",data,"FROM",address if __name__ == '__main__': client = Client() client.connect() while 1: client.send_data() time.sleep(5) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload