Socket server

Jonathan Gardner jgardn at alumni.washington.edu
Thu Jan 17 22:32:07 CET 2002


On Friday 18 January 2002 03:39 am, Hugo Martires wrote:
> I have a server that answer a single client, but now i need moore
> clients.
>
> Witch is the best way to make a server socket that answer's the call's
> of many clients ?
> I have to use threads ? Queues ?
>
> Can any one send me a simple example ?
>

"Simple Example" - it's not so simple anymore. Depending on what you want to 
do, your options are:

- Forking. Each new call is answered by forking off a child process. The 
child process does all the work, and when it is done, it dies. This is how 
Apache does it. You can get it into all kinds of fun problems, like keeping 
some children alive, telling those children to die after so many times, 
etc... Your level of complexity depends on what you want.

- Threading. Each new call is answered with a new thread. This entails all 
that threading is about. It gives you similar results as the above, except 
each thread can play with the parent in the way that threads like to do.

- Multiplexing. You run in a tight loop, waiting until there is something to 
read or until one of the sockets is ready to write to. The simplest example 
of this is much more complicated than the above two, but it really isn't that 
hard. This is the best bet for MUDs and such. And you can't really make it 
much better. However, windows doesn't treat sockets like files, and so you 
run into some interesting problems on windows that just aren't issues on a 
real operating system. (Like, you can run a select call on stdin, stdout, and 
stderr.)

Code for each of these abounds on the internet. Take a google search. Look 
through the modules for SocketServer and its children. You will find the 
modules remarkable readable, even to a newbie.

Jonathan




More information about the Python-list mailing list