Skip to content Skip to sidebar Skip to footer

Python Client Sockets Connecting On The Same Server Socket Port

I am working for the first time on sockets with python. I need to connect more than a client socket to the same server socket. To do this I used the the following code: import sock

Solution 1:

I would highly recommend using the excellent socketserver module for doing stuff like this. As for your code, the problem is that your threaded socket server only runs in a single thread. At the very least, you should create a new thread to handle the connection after accepting the new connection in openSocketConnectionAsServer.

Solution 2:

What @Ber said was correct but incomplete.

You're problem is here:

self.socket.listen(5)
self.conn, addr = self.socket.accept()

listen will open up the port and prepare to receive connections. accept will wait for the next connection.

While you only need to call listen once, you must call accept multiple times, once per connection. There are several ways you can arrange this. For starters you could call accept again when the current connection closes, but this will only allow one client at a time. Better to have one thread call accept to wait for the next connection, but have it start a worker thread to handle each one.

Or you could use non-blocking I/O, but if you go this way, check out Twisted.

Post a Comment for "Python Client Sockets Connecting On The Same Server Socket Port"