I have a server that can handle only ONE client. The client gets authenticated, then the client can store information and perform a bunch of other stuff

Now, I have to make my server compatible to multiple clients.. up to 10 clients.
and the clients can work simultaneously with out knowing each other.

How should i implement threads. I just want a suggestion

03-27-2011

CommonTater

On which operating system?

Different OSs handle threads in differing ways....

03-27-2011

Codeplug

Don't use threads at first - learn to multiplex within a single thread using select/poll and non-blocking I/O.

gg

03-27-2011

MK27

Quote:

Originally Posted by Codeplug

Don't use threads at first - learn to multiplex within a single thread using select/poll and non-blocking I/O.

gg

Ditto! A server can connect to multiple clients, each with it's own connection. You use select() to wait for a message from any of the clients and deal with them as they come in.

The only reason to use threads/parallelism is if the server is to perform some time consuming process occasionally and you don't want to leave anyone waiting. In that case, it is this processes which forks off -- there is still only one server listening on one socket. Even apache works that way: there is a main process which handles all incoming calls, and acts like a switchboard for children who perform tasks.

Remember, even with threads working "simultaneously" is sort of an illusion. It is really a matter of cycling quickly between short tasks. A server loop using select accomplishes much the same thing:

Code:

while (1) {
select(); // wait for a message
/* here's where you sort the messages out according to content and origin,
handing off to whatever functions are appropriate */
}