Socket won't unbind from address when socket is terminated
-
After my successive failures using bluetooth to get my wipy's to communicate, I switched to using wifi and sockets. It does work, however using basically example code from the documentation I cannot soft reset and rerun the module without getting the error (from server wipy):
OSError: [Errno 112] EADDRINUSEcode used:
client:def socket_thread(p): sconn =True while sconn: # Wait for any action to happen infinitely l = p.poll() # Start processing the actions happened for t in l: # First element of the returned tuple is the socket itself sock = t[0] # Second element of the returned tuple is the events happened event = t[1] # If any error or connection drop happened close the socket if(event & uselect.POLLERR or event & uselect.POLLHUP): sock.close() sconn = False continue # If the socket is writable then send some data # The socket becomes writable if the connect() was successfull if(event & uselect.POLLOUT): # If any error occurs during sending here, do "nothing", poll() will return with error event, close the socket there try: sock.send("Data to send") # We only want to send one message on this socket, in the future wait only for new incoming messages p.modify(sock, uselect.POLLIN | uselect.POLLHUP | uselect.POLLERR) except: pass # If any new data is received then get it if(event & uselect.POLLIN): # If any error occurs during receiving here, do "nothing", poll() will return with error event, close the socket there try: r = sock.recv(12) # If recv() returns with 0 the other end closed the connection if len(r) == 0: sock.close() sconn = False continue else: # Do something with the received data... print("Data received: " + str(r)) except: pass def makesockets(): telem = socket.socket() telem.setblocking(False) # Create a new poll object p = uselect.poll() # Register the sockets into the poll object, wait for all kind of events p.register(telem, uselect.POLLIN | uselect.POLLOUT | uselect.POLLHUP | uselect.POLLERR) try: telem.connect(socket.getaddrinfo("192.168.4.1", 1235)[0][-1]) print('socket created') except OSError as e: if e.args[0] != uerrno.EINPROGRESS: raise _thread.start_new_thread(socket_thread, (p,))
server:
def client_thread(clientsocket): # Receive maxium of 12 bytes from the client r = clientsocket.recv(12) # If recv() returns with 0 the other end closed the connection if len(r) == 0: clientsocket.close() return else: # Do something wth the received data... print("Received: {}".format(str(r))) # Sends back some data clientsocket.send('nice') # Close the socket and terminate the thread clientsocket.close() # Set up server socket def createsocket(): serversocket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) serversocket.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1) serversocket.bind(("192.168.4.1", 1235)) serversocket.settimeout(100) serversocket.listen(1) (clientsocket, address) = serversocket.accept() _thread.start_new_thread(client_thread, (clientsocket,))
-
I tried adding that but it was unsuccessful- however, the repl is staying open (not giving me a >>>) on my server after receiving a message, so it isn't finishing out whatever it's stuck open doing I think. I don't see any reason why it shouldn't finish out though? Doesn't the clientsocket.close() kill the thread?
-
You can re-initialize the socket after closing it by calling
telem= socket.socket()
again. This should prevent the error you are getting