M
Matt Stevens
Hello,
I'm having some trouble trying to get this simple forking http daemon to
work, when I run it everything seems to run ok and then when I try to
connect to it with a browser on another machine it just spits out hundreds
of error messages every few seconds.
Heres an example:
[error]
print() on closed filehandle GEN1 at ./httpd.pl line 31 (#1)
getpeername() on closed socket GEN1 at
/usr/local/lib/perl5/5.8.2/mach/IO/Socket.pm line 199 (#2)
[/error]
well anyway, here is the program:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use IO::Socket;
$SIG{CHLD} = sub { wait };
my (
$socket, $client,
$pid
);
$socket = IO::Socket::INET->new(LocalPort => 8080,
Proto => 'tcp',
Listen => 5,
Reuse => 1);
die "Could not create socket: $!\n" unless $socket;
while ($client = $socket->accept()) {
die unless defined($pid = fork);
# parent goes to continue block
next if $pid;
# flush all output to client
select $client;
$|++;
print "HTTP/1.0 200 OK\r\n";
print "Content-type: text/html\r\n\r\n";
print "Your IP Address: ", $client->peerhost;
close($client);
exit;
} continue {
close($client);
# go back to the while loop, and wait for a connection
redo;
}
Any help would be appreciated,
Matt.
I'm having some trouble trying to get this simple forking http daemon to
work, when I run it everything seems to run ok and then when I try to
connect to it with a browser on another machine it just spits out hundreds
of error messages every few seconds.
Heres an example:
[error]
print() on closed filehandle GEN1 at ./httpd.pl line 31 (#1)
getpeername() on closed socket GEN1 at
/usr/local/lib/perl5/5.8.2/mach/IO/Socket.pm line 199 (#2)
[/error]
well anyway, here is the program:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use IO::Socket;
$SIG{CHLD} = sub { wait };
my (
$socket, $client,
$pid
);
$socket = IO::Socket::INET->new(LocalPort => 8080,
Proto => 'tcp',
Listen => 5,
Reuse => 1);
die "Could not create socket: $!\n" unless $socket;
while ($client = $socket->accept()) {
die unless defined($pid = fork);
# parent goes to continue block
next if $pid;
# flush all output to client
select $client;
$|++;
print "HTTP/1.0 200 OK\r\n";
print "Content-type: text/html\r\n\r\n";
print "Your IP Address: ", $client->peerhost;
close($client);
exit;
} continue {
close($client);
# go back to the while loop, and wait for a connection
redo;
}
Any help would be appreciated,
Matt.