Forking HTTP Daemon - Problem

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.
 
J

Jim Gibson

Matt said:
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.

From perldoc -f redo:

"The "redo" command restarts the loop block without evaluating
the conditional again. The "continue" block, if any, is not
executed. If the LABEL is omitted, the command refers to the
innermost enclosing loop. This command is normally used by
programs that want to lie to themselves about what was just
input:"

It is the conditional of the while loop ($client = $socket->accept())
that performs the accept. The redo causes that to be skipped and goes
right to the "die unless ...". So just get rid of the redo. You don't
need it anyway - the loop will continue without it. And it shouldn't be
in a continue block in any case.

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future
for better response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top