mortenal has asked for the wisdom of the Perl Monks concerning the following question:
use IO::Socket;
use IO::Select;
#require 'c:\perl\banner.pl';
sub spawn; #forward declaration
# Create a socket to listen on.
#
my $serverA = #handles ad requests
IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
my $serverL = #handles log requests
IO::Socket::INET->new( LocalPort => 8009, Listen => 5, Reuse => 1 );
die "Can't create socket for listening: $!" unless $serverA;
print "Listening for connections on port 8008\n";
die "Can't create socket for listening: $!" unless $serverL;
print "Listening for connections on port 8009\n";
my $readable = IO::Select->new; # Create a new IO::Select object
$readable->add($serverA); # Add the servers to it
$readable->add($serverL);
while(1) {
# Get a list of sockets that are ready to talk to us.
#
my ($ready) = IO::Select->select($readable, undef, undef, undef);
#***
foreach my $s (@$ready) {
# Is it a new connection?
#
if($s == $serverL) {
# Grab the request, close the socket, and log it as fast as we can
#
my $new_sock = $serverL->accept;
my $in=<$new_sock>;
$new_sock->close;
# slog($in);
print $in;
} elsif($s == $serverA) {
# Fork off, get the request, and reply with the banner
#
my $new_sock = $serverA->accept;
spawn $new_sock;
} else { # It's an established connection, and shouldn't be here
$s->close;
}
}
}
sub spawn {
my $client=shift;
my $pid;
if (!defined($pid = fork)) {
die "can't fork";
return;
} elsif ($pid) {
return; # I'm the parent
}
$in=<$client>; #get the request
# print $client &process_request($in);
print $client $in;
$client->close;
exit;
}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems with fork or socket code???
by tye (Sage) on Aug 27, 2000 at 08:58 UTC | |
by mortenal (Novice) on Aug 27, 2000 at 11:38 UTC | |
by mortenal (Novice) on Aug 27, 2000 at 22:02 UTC | |
|
Re: Problems with fork or socket code???
by tye (Sage) on Aug 28, 2000 at 00:46 UTC | |
|
And the solution is...
by mortenal (Novice) on Aug 28, 2000 at 01:33 UTC |