chorg has asked for the wisdom of the Perl Monks concerning the following question:

I need to make a server quick to implement some stuff on a site. I've read perlipc and the camel book, but I now need some detailed information on where to look next. My questions are:

Is there a handy "This is how you make server apps in perl page anywhere?"

Which is best - forking or multiplexing requests (I'm looking at a lot of connections potentially) and is there a point where I would have to switch?

What major gotchas await the newbie server creator?

Tbanks,

chorg

---------
"Intelligence is a tool used achieve goals, however goals are not always chosen wisely..."

Replies are listed 'Best First'.
Re: Max Clients on a Perl Server
by cephas (Pilgrim) on Nov 17, 2000 at 06:57 UTC
    You should take a look at IO::Socket, or if your familiar with sockets in C (and don't want to do it the easy way), then take a look at Socket.pm
    Also, if you considering handling it without forking, take a look at IO::Select (again, if you're a glutton for punishment, just use select and have fun with your vectors)
    As to whether multiplexing, or forking is better, it kinda depends on preference, and on the application. I generally prefer to do forking when I have my choice or either will do.
    When doing forking, you need to decide if you want to fork as needed, or do preforking (similar to what Apache normally does), also you'll want to keep track of your children (you don't want to fill up your process table do you?). Also, don't forget to set $SIG{CHLD} properly to deal with dead children

    I personally feel that forking on demand is the easiest set up, but multiplexing can give you some tighter control (for instance, if for each connection, you'll be accessing a single resource constantly, a multiplexed server would be easiest to eliminate contention)

    I believe there are also some good examples in the Perl Cookbook, and in Advanced Perl Programming (both from O'Reilly of course)

    Hope that helps,
    cephas
Re: Max Clients on a Perl Server
by cephas (Pilgrim) on Nov 17, 2000 at 07:49 UTC
    Here's an example using IO::Select and IO::Socket....
    #!/usr/bin/perl use IO::Select; use IO::Socket; $sock = IO::Socket::INET->new(LocalHost => '127.0.0.1', LocalPort => 9 +999, Proto => 'tcp', Listen => 10, Reuse => 1) || die("Couldn't create socket: $ +!\n"); $s = IO::Select->new($sock); while(1) { foreach $client ($s->can_read(0)) { #check for all ready sockets if($client == $sock) { #if this is the main socket #we'll try to accpet a new connection $client = $sock->accept(); $s->add($client); } else { #this is a client socket if(eof($client)) { $s->remove($client); #remove them from the list if we've hit eof } $line = <$client>; print($client->peerhost, ": said $line"); } } }
    cephas
Re: Max Clients on a Perl Server
by dryland (Initiate) on Nov 17, 2000 at 05:23 UTC
    cgi101.com is a very quick cgi reference.. you can also look at:
    http://www.jmarshall.com/easy/cgi/

    for more comprehensive perl stuff maybe: http://www.codebits.com/p5be/ch10.cfm or wdvl.com

    max clients really depends on your server/bandwidth.. if you want to increase the speed of your perl look into mod_perl, wdvl has some decent articles on the subject..
Re: Max Clients on a Perl Server
by cephas (Pilgrim) on Nov 17, 2000 at 07:19 UTC
    Here's some code for the forking server (not prefork), I'll try to wack out some code using IO::Select shortly....
    #/usr/bin/perl use IO::Socket; $SIG{'CHLD'} = \&REAPER; #Set up our signal handler $sock = IO::Socket::INET->new(LocalHost => '127.0.0.1', LocalPort => 9 +999, Proto => 'tcp', Listen => 10, Reuse => 1) || die("Couldn't create socket: $ +!\n"); while($client = $sock->accept()) { #our loop to take the connections $pid = fork(); if($pid == 0) { #Child process, do work while(defined($line = <$client>)) { print($client->peerhost, ": said $line\n"); } } } close($sock); sub REAPER { 1 until (waitpid(-1,WNOHANG) == -1); #We do it this way to avoid chil +dren #dying before our signal handler + gets #reinstalled $SIG{'CHLD'} = \&REAPER; }
Re: Max Clients on a Perl Server
by cianoz (Friar) on Nov 17, 2000 at 20:51 UTC
    have a look at NetServer::Generic (available on CPAN) for a quick way of implementing a server program. (it supports both the forking and the select loop model, so you can try both by just setting a variable)
Re: Max Clients on a Perl Server
by dryland (Initiate) on Nov 17, 2000 at 05:28 UTC
    here's the link for optimizing with mod_perl on wdvl:
    http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/index14.html
      and another...:

      http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/optimization.html
Re: Max Clients on a Perl Server
by dws (Chancellor) on Nov 17, 2000 at 06:31 UTC
    Please expand on what you mean by "server apps in a perl page". Are you thinking of forking off a server process from a CGI?