nickponline
nickponline

Reputation: 25914

How do I receive and manage multiple TCP connections on the same port?

I have a number of clients who need to connect to a server and maintain the connection for some time (around 4 hours). I don't want to specify a different connection port for each client (as there are potentially many of them) I would like them just to be able to connect to the server on a specific predetermined port e.g., 10800 and have the server accept and maintain the connection but still be able to receive other connections from new clients. Is there a way to do this in Python or do I need to re-think the architecture.

EXTRA CREDIT: A Python snippet of the server code doing this would be amazing!

Upvotes: 0

Views: 1720

Answers (2)

user207421
user207421

Reputation: 310874

I don't want to specify a different connection port for each client (as there are potentially many of them)

You don't need that.

I would like them just to be able to connect to the server on a specific predetermined port e.g., 10800 and have the server accept and maintain the connection but still be able to receive other connections from new clients

That's how TCP already works.

Just create a socket listening to port 10800 and accept connections from it.

Upvotes: 1

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17415

Use select.select() to detect events on multiple sockets, like incoming connections, incoming data, outgoing buffer capacity and connection errors. You can use this on multiple listening sockets and on established connections from a single thread. Using a websearch, you can surely find example code.

Upvotes: 0

Related Questions