Forking HTTP Daemon - Problem

Discussion in 'Perl' started by Matt Stevens, Dec 8, 2003.

  1. Matt Stevens

    Matt Stevens Guest

    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.
    Matt Stevens, Dec 8, 2003
    #1
    1. Advertising

  2. Matt Stevens

    Jim Gibson Guest

    In article <JdRAb.11920$>, Matt
    Stevens <> 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.
    Jim Gibson, Dec 9, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reid Priedhorsky

    Forking SocketServer daemon -- updating state

    Reid Priedhorsky, Feb 19, 2007, in forum: Python
    Replies:
    2
    Views:
    275
    Nick Craig-Wood
    Feb 20, 2007
  2. Scottman

    Daemon Server, Forking, Defunct Processes

    Scottman, Sep 11, 2008, in forum: C Programming
    Replies:
    2
    Views:
    925
    CBFalconer
    Sep 11, 2008
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    448
    Jean-Paul Calderone
    Mar 20, 2009
  4. Floris Bruynooghe
    Replies:
    1
    Views:
    443
    Floris Bruynooghe
    Mar 24, 2009
  5. Daemon Win32::Daemon;

    , Sep 7, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    240
Loading...

Share This Page