I am working on a multi-user server that should handle more than 1000
In the beginning I had to decide which one of the 2 main methods to
use. The select() / poll() in a single process, OR thread per user
I went with the select() / poll() cuz I figured, at the servers max
usage, the scheduler will spend more time running than the actual
threads themselves. I know that there is not that much overhead
involved in threads as they do in presses sched.
Ok , here is how I built the foundation.
I have a user() class and an array of pointers to user() class.
In my loop I call select with all users inserted in the read set.
After select returns, I go through the array one by one and check to
see if the user has any data to read, if yes I process the data, and
append it to a send buffer for the user, than I check to see if the
user is write ready on socket, if yes I send data, after the send call
I check again if there is any data left in the send buffer,
if yes, I insert user into send set (FD_SET()).
And I go on to the next user. So on and so on until I go through the
But now that I look at it, certain users get priority over other users
because they come first in the array before others. It also could
cause problems, not as in (BUGS) but something like this, if
array[n]->user and array[n+5] both returned from select
at the same time, than the array[n]->user gets priority over
array[n+5]->user even though array[n+5]->users data MIGHT have
arrived earlier. In a chat server this would suck.
Another problem I am starting to encounter is the send buffer. Lets
say I have a 500 user chat server. All 500 users are connected. I call
select() with all 500 of them in the set. Sure not all 500 of them
will be ready for reading, but lets say 30 of them are. So according
to the algorithm on top, when I go through the array and process
data, some data is required to be sent to all users, so I append to
each users send buffer. So until the next select call some of the
users send buffers get to be very huge. I have solved half of this
problem by using dynamic memory. But I figure there has to be a better
I tried to come up with a method to send data to users as soon as they
are processed instead of appending them to a send buffer, BUT you wont
know if the users socket is ready to be written yet, until you call
If anybody has a better way of building a multi-user server
architecture, please post your ideas. Any kind of improvement on the
algorithm above will be appreciated.