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;
}In reply to Problems with fork or socket code??? by mortenal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |