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 | [reply] |
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
| [reply] [d/l] |
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..
| [reply] |
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;
}
| [reply] [d/l] |
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) | [reply] |
here's the link for optimizing with mod_perl on wdvl:
http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/index14.html | [reply] |
and another...:
http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/optimization.html
| [reply] |
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?
| [reply] |