I have a server-client application written in python. Everything worked fine but I wanted to make the server multi threaded and everything crashed.
Here is part of the code for the server:
host = 'localhost'
port = 10001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
class ClientThread(threading.Thread):
def __init__(self, ip, port, socket):
print '5'
threading.Thread.__init__(self)
self.ip = ip
self.port = port
self.socket = socket
print "[+] New thread started for "+ip+":"+str(port)
def __run__(self):
while True:
try:
#conn, addr = sock.accept()
print >>sys.stderr, "Connection from : "+ip+":"+str(port)
print '6'
#reqCommand = conn.recv(1024)
reqCommand = self.recv(1024)
print '7'
command = reqCommand.split(' ', 1) # get <<filename>>
print '8'
reqFile = command[1] # takes the name of the file
reqCommand = command[0]
print '9'
encFile = reqFile + "_enc"
print >>sys.stderr, 'Client> %s' % (reqCommand)
if (reqCommand == 'get'):
pass
try:
os.remove(encFile) # removes the encrypted file
except OSError, e:
print ("Error: %s - %s." % (e.filename,e.strerror))
print >>sys.stderr, 'successfully finished'
print >>sys.stderr, 'waiting for new connections...'
finally:
# clean up connection
self.close()
while True:
sock.listen(4)
print "\nListening for incoming connections..."
(conn, (ip, port)) = sock.accept()
print '1'
newthread = ClientThread(ip, port, conn)
print '2'
newthread.start()
print '3'
threads.append(newthread)
print '4'
When I type in the client: "get " it sends the message to the client but it doesn't receive anything back. In the server as you can see I have a lot of prints to see where it crashes. It prints in the following order: 1 5 2 3 4. + it also prints [+] new thread...
As you can also see I've used self.recv instead of conn.recv (this was a solution that I found on stackoverflow, but it didn't work)
Does anyone have a clue what I'm doing wrong? I mention again that before I've added threads and the class ClientThread everything worked fine. Thanks in advance!
__init__?