Note that there are some explanatory texts on larger screens.

plurals
  1. POpython socket server/client protocol with unstable client connection
    primarykey
    data
    text
    <p>I have a threaded python socket server that opens a new thread for each connection.</p> <p>The thread is a very simple communication based on question and answer. Basically client sends initial data transmission, server takes it run an external app that does stuff to the transmission and returns a reply that the server will send back and the loop will begin again until client disconnects.</p> <p>Now because the client will be on a mobile phone thus an unstable connection I get left with open threads no longer connected and because the loop starts with recv it is rather difficult to break on lost connectivity this way.</p> <p>I was thinking on adding a send before the recv to test if connection is still alive but this might not help at all if the client disconnects after my failsafe send as the client sends a data stream every 5 seconds only.</p> <p>I noticed the recv will break sometimes but not always and in those cases I am left with zombie threads using resources.</p> <p>Also this could be a solid vulnerability for my system to be DOSed. I have looked through the python manual and Googled since thursday trying to find something for this but most things I find are related to client and non blocking mode.</p> <p>Can anyone point me in the right direction towards a good way on fixing this issue?</p> <p><strong>Code samples:</strong></p> <p>Listener:</p> <pre><code>serversocket = socket(AF_INET, SOCK_STREAM) serversocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) serversocket.bind(addr) serversocket.listen(2) logg("Binded to port: " + str(port)) # Listening Loop while 1: clientsocket, clientaddr = serversocket.accept() threading.Thread(target=handler, args=(clientsocket, clientaddr,port,)).start() # This is useless as it will never get here serversocket.close() </code></pre> <p>Handler:</p> <pre><code> # Socket connection handler (Threaded) def handler(clientsocket, clientaddr, port): clientsocket.settimeout(15) # Loop till client closes connection or connection drops while 1: stream = '' while 1: ending = stream[-6:] # get stream ending if ending == '.$$$$.': break try: data = clientsocket.recv(1) except: sys.exit() if not data: sys.exit() # this is the usual point where thread is closed when a client closes connection normally stream += data # Clear the line ending stream = base64.b64encode(stream[:-6]) # Send data to be processed re = getreply(stream) # Send response to client try: clientsocket.send(re + str('.$$$$.')) except: sys.exit() </code></pre> <p>As you can see there are three conditions that at least one should trigger exit if connection fails but sometimes they do not.</p>
    singulars
    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.
    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