Forking server

Discussion in 'Perl' started by SRam, Aug 21, 2003.

  1. SRam

    SRam Guest

    How to create a threaded process in Perl. I need explanation for this
    code from Advanced Perl Programming....

    # Forking server
    use IO::Socket;
    $SIG{CHLD} = sub {wait ()};
    $main_sock = new IO::Socket::INET (LocalHost => 'goldengate',
    LocalPort => 1200,
    Listen => 5,
    Proto => 'tcp',
    Reuse => 1,
    );
    die "Socket could not be created. Reason: $!\n" unless ($sock);
    while ($new_sock = $main_sock->accept()) {
    $pid = fork();
    die "Cannot fork: $!" unless defined($pid);
    if ($pid == 0) {
    # Child process
    while (defined ($buf = <$new_sock>)) {
    # do something with $buf ....
    print $new_sock "You said: $buf\n";
    }
    exit(0); # Child process exits when it is done.
    } # else 'tis the parent process, which goes back to accept()
    }
    close ($main_sock);
    SRam, Aug 21, 2003
    #1
    1. Advertising

  2. SRam

    dw Guest

    "SRam" <> wrote in message
    news:...
    > How to create a threaded process in Perl. I need explanation for this
    > code from Advanced Perl Programming....


    First of all... you probably should take this over to comp.lang.perl.misc.
    People complain that this group doesn't exist, you you may get better
    answers over there....

    >
    > # Forking server
    > use IO::Socket;
    > $SIG{CHLD} = sub {wait ()};
    > $main_sock = new IO::Socket::INET (LocalHost => 'goldengate',
    > LocalPort => 1200,
    > Listen => 5,
    > Proto => 'tcp',
    > Reuse => 1,
    > );
    > die "Socket could not be created. Reason: $!\n" unless ($sock);


    I'm guessing that $sock should be $main_sock

    > while ($new_sock = $main_sock->accept()) {
    > $pid = fork();
    > die "Cannot fork: $!" unless defined($pid);
    > if ($pid == 0) {
    > # Child process
    > while (defined ($buf = <$new_sock>)) {
    > # do something with $buf ....
    > print $new_sock "You said: $buf\n";
    > }
    > exit(0); # Child process exits when it is done.
    > } # else 'tis the parent process, which goes back to accept()
    > }
    > close ($main_sock);


    Advanced Perl Programming has a short description after the segment of code.
    You didn't say what you don't understand: the code, the way a server works,
    why the code always died? If you say what you don't understand... maybe you
    can get a better answer.
    dw, Aug 21, 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. SRam

    Forking Server

    SRam, Sep 15, 2003, in forum: Perl
    Replies:
    0
    Views:
    542
  2. Ed
    Replies:
    0
    Views:
    686
  3. Replies:
    3
    Views:
    607
    Thomas Guettler
    Jun 12, 2006
  4. Scottman

    Daemon Server, Forking, Defunct Processes

    Scottman, Sep 11, 2008, in forum: C Programming
    Replies:
    2
    Views:
    946
    CBFalconer
    Sep 11, 2008
  5. SRam

    Forking Server

    SRam, Sep 15, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    111
Loading...

Share This Page