Forking HTTP Daemon - Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Stevens

    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::INE T->new(LocalPor t => 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.



  • Jim Gibson

    #2
    Re: Forking HTTP Daemon - Problem

    In article <JdRAb.11920$SV 1.2657@news-binary.blueyond er.co.uk>, Matt
    Stevens <matt@zola.org. uk> wrote:
    [color=blue]
    > 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::INE T->new(LocalPor t => 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.[/color]

    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.

    Comment

    Working...