[Van-pm] Question about handling multiple clients

Kevin Scaldeferri kevin at scaldeferri.com
Fri Mar 31 10:06:42 PST 2006


On Mar 31, 2006, at 9:02 AM, Heng Ha wrote:

> Hello everyone,
>
>    I am coding a daemon which should handle multiple
> clients.
>
>    I referenced the pre-forking server code in Perl
> cookbook that pre-forks several child processes and
> each of them handle multiple requests.
>
>    The problem I got now is in the code the counter
> calculates the number of incoming requests never
> increments.

The problem is that accept() will almost always return a true value, so 
that while loop never exits.  You don't have a timeout, so the only 
reason it should ever fail is if the local socket is closed for some 
reason or something borks the networking.  Otherwise, it just blocks 
until a connection comes in.

To get the effect you want, you should combine the outer two loops or 
just get rid of the accept loops and do

   $client = $server->accept() or last;

which is actually what my copy of the cookbook has in this example.



>
>    The code is the following and can any one give me a
> hand ? also, if possible, anyone has experience with
> thread programming in perl, is that production ready ?

Don't do it.

If you need thread-like parallelism, POE is a much better idea.

>
>           # handle connections until we've reached
> $MAX_CLIENTS_PER_CHILD
>           for ($i=0; $i < $MAX_CLIENTS_PER_CHILD;
> $i++)      {
>              while ( $client = $server->accept( ) )
>                   {
>                     while ( <$client> )
>                       {
>                        #do something like print
> $client....
>
>                       }
>                        $client->shutdown(SHUT_RDWR);
>                   }
>               # do something with the connection
>           }
>



-kevin



More information about the Vancouver-pm mailing list