The usual way to do this is to have a parent process that is essentially always blocked on accept, with child processes that do the actual work. Fitting this into your framework would be something like this:
One thing missing from here is a throttle. You want to keep a count of active child processes so you don't keep creating new ones without any limit. Incrementing the counter is easy; the trick is to decrement the counter properly when each child process exits. In unix-land you can trap SIGCHLD for that, I don't know the equivalent on Win32. Whichever method you use, be careful to single-thread access to the counter. In other words, make sure when two children exit "at the same time", the counter really is decremented twice.while ($client = $server->accept()) { $pid = fork(); if ($pid == 0) { print $client "Welcome to $0"; # etc. # Probably a loop here reading from $client # ... Other stuff exit 0; # Remember to go away when done! } # parent process here close $client; } # and go back to accept.
I haven't looked at the modules for handling forks. They may well solve this problem for you.
HTH
In reply to Re: Creating a server that accepts more than one client.
by VSarkiss
in thread Creating a server that accepts more than one client.
by boo_radley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |