Steve P.
Steve P.

Reputation: 14709

Python socket support/threading for client-server model

So, I'm trying to write a very simple Client-Server system that places some congestion control over UDP. Simply, the server sends data packets and receives feedback packets from the client(s). I'm fairly new to this and am not certain about:

(1) How many users can be connected to the same socket?

(2) Can a socket be used by multiples users simultaneously (aka do reads/writes in parallel)?

(3) I need to use threading so that for each user, I can send data packets to them and receive feedback packets from them in parallel. I read in another post that one shouldn't be opening threads for each user (and I'm opening two threads per user). Am I wrong in doing this? Do I have an inherent misunderstanding? Does anyone have a suggestion for how to handle this?

Sorry, I'm very new to this.

Upvotes: 1

Views: 780

Answers (2)

A. Rodas
A. Rodas

Reputation: 20689

According to the documentation of socket.listen():

Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 0; the maximum value is system-dependent (usually 5), the minimum value is forced to 0.

So technically the request aren't processed in parallel but queued, until a usual maximum of 5. I don't know the reasons the author of the post you mention has to state that you shouldn't start a thread per user, but I have read that most servers use one thread per request. Also the thread pool is a common pattern, and it can be easily implemented using a syncronized object like Queue (this example may be useful if you want to see a small implementation).

Upvotes: 1

joamag
joamag

Reputation: 1472

(1) The number of connections to a socket is limited by the operative system but can go up to 100k and more check the C10K problem

(2) A socket may be used by multiple threads and even multiple processes using the pre-fork approach

(3) You may use a new thread per connection, but the best approach is to use a thread pool, alternatively you may use an asynchronous approach and avoid the use of threads

Upvotes: 2

Related Questions