0

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!

3
  • so it doesn't reach the end of __init__? Commented Oct 26, 2015 at 21:22
  • no, sorry. it also prints that. it seems that it doesn't enter __run__() Commented Oct 26, 2015 at 21:26
  • stackoverflow.com/questions/5520821/… Commented Oct 26, 2015 at 21:32

1 Answer 1

1

You have a lot of errors in the code shown.

E.g. self.recv(1024) should probably be replaced with self.socket.recv(1024), and self.close() with self.socket.close()? (since self is an instance of ClientThread/Thread, not a socket). I also think run method should be named just run (not __run__), and if you do a close() in the finally in run() the second time while True is executed connction will be already closed.

In addition to that, large chunks are missing, e.g. all the imports, and a call to bind() - e.g. something like sock.bind((socket.gethostname(), port))

Other than that and assuming all these errors are fixed, it seems that it should do what it is supposed to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.