472,146 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Forking HTTP Daemon - Problem

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.

Jul 19 '05 #1
1 4397
In article <Jd******************@news-binary.blueyonder.co.uk>, Matt
Stevens <ma**@zola.org.uk> wrote:
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.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Eric S. Johansson | last post: by
reply views Thread by SRam | last post: by
7 posts views Thread by Michael Ransburg | last post: by
3 posts views Thread by felixfix | last post: by
1 post views Thread by Stefan Neumann | last post: by
3 posts views Thread by czajnik | last post: by
2 posts views Thread by Reid Priedhorsky | last post: by
10 posts views Thread by qwertycat | last post: by
3 posts views Thread by Scottman | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.