linux - tcp server in python not handling multiple clients -


i need tcp server run on raspberry pi allow multiple simultaneous client connections, process requests, allow clients disconnect gracefully, , continue listen forever. found code on older post here close has few problems cannot find solution for. server script is:

import socket,threading  tip   = '192.168.99.161' tport = 3006 buffer_size = 20  class clientthread(threading.thread):      def __init__(self,ip,port,socket):         threading.thread.__init__(self)         self.ip = ip         self.port = port         self.socket = socket         print "[+] new thread started "+ip+":"+str(port)       def run(self):             print "connection : "+ip+":"+str(port)         clientsock.send("\nrpi02\n")         data = "111"         eof=false         while len(data):             data = clientsock.recv(20)             c in data:                 print ("%02x"%(ord(c)))                 if(c == 'z'):                 eof=true             if(eof):                 # simulated packet:                             clientsock.send("packet processed.")                 eof = false          print "%s:%d disconnected"%(ip,port)  host = "0.0.0.0" port = 9999  tcpsock = socket.socket(socket.af_inet, socket.sock_stream) tcpsock.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1)  tcpsock.bind((tip,tport)) threads = []  while true:     tcpsock.listen(4)     print "\nlistening incoming connections..."     (clientsock, (ip, port)) = tcpsock.accept()     newthread = clientthread(ip, port,clientsock)     newthread.start()     threads.append(newthread)  t in threads:     t.join()  tcpsock.shutdown(sock.shut_rdwr) tcpsock.close() 

if run 1 client, can process data (send many bytes), no problem.

the expectation of system don't expect ever have more 2 clients connected. when client connects, send 3-4 bytes of request data, , once receives answer, disconnect (another pi on client end).

i ran test 2 client windows, 1 sending '123', other sending 'abc'. sending 'z' client gets server message confirming processed packet.

here's test run:

mpi@rpi01 ~/iorelay $ python erc-test.py  listening incoming connections... [+] new thread started 192.168.99.99:64351  listening incoming connections... connection : 192.168.99.99:64351 31 32 33 7a [+] new thread started 192.168.99.99:64361  listening incoming connections... connection : 192.168.99.99:64361 61 62 63 7a 31 61 62 63 61 62 63 192.168.99.99:64361 disconnected 192.168.99.99:64361 disconnected 

i see several problems here:

  1. the second (or last) client connect can send data day long, fake-packet response server. previous clients may send few more bytes (one in case) server stops receiving them.

  2. i disconnected first client, , no disconnect message appeared. when closed second one, printed 2 disconnect messages same client port number.

  3. i don't understand 't.join' loop after main while() - for? if comment out doesn't seem change anything.

  4. even shutdown() call @ end, when close script ctl-z socket evidently hangs. when run script again, complains socket in use.

  5. how clean hung sockets in linux? (rasbian on pi)

thanks on - still learning python...


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -