$ perl server-fork.pl 1 &
$ time perl client.pl 1000 > /dev/null
real 0m0.163s
user 0m0.093s
sys 0m0.021s
####
$ perl server-fork.pl 0 &
$ time perl client.pl 1000 > /dev/null
real 0m40.018s
user 0m0.052s
sys 0m0.015s
####
use strict;
use IO::Select;
use IO::Socket::INET;
$SIG{CHLD} = 'IGNORE';
my $one_print = shift @ARGV || 0;
my $listener = IO::Socket::INET->new(Listen => 1, LocalPort => 8080);
my $select = IO::Select->new($listener);
while( my @ready = $select->can_read ){
foreach( @ready ){
if( $_ == $listener ){
my $sock = $listener->accept;
my $pid = fork;
if( defined $pid && $pid == 0 ){
while( 1 ){
my $line = <$sock>;
last unless defined $line;
if( $line =~ /^QUIT/ ){
print $sock "Goodbye\n";
$sock->close;
last;
}elsif( $one_print ){
print $sock $line, ".\n";
}else{
print $sock $line;
print $sock ".\n";
}
} # while read loop
exit(0);
}
}
}
}
####
use strict;
use IO::Socket::INET;
my $trials = shift @ARGV;
my $sock = IO::Socket::INET->new(PeerHost => 'localhost', PeerPort => 8080);
for(1..$trials){
my $tm = time;
print $sock "Hello\n";
while(my $line = <$sock>){
last if $line eq ".\n";
print $line;;
}
}
print $sock "QUIT\n";