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 */
}