in reply to RE: Sockets
in thread Sockets

Curious, what did this snippet do for you?
$SIG{CHLD} = sub {wait()};

and what about this part?
} else { $line = <STDIN>; print $new_sock "$line"; }


Also, how do you get the server to hang up on the client? I tried something like:
chomp($buf); exit(0) if ($buf eq 'q');

But the client (I use telnet) connected to the server holds fast until I hit <CR> on the console that's running the server.

Replies are listed 'Best First'.
Re: Re: RE: Sockets
by clemburg (Curate) on Apr 24, 2001 at 14:26 UTC

    Well, I just copied the code over from the question to illustrate, but ...

    • $SIG{CHLD} = sub {wait()};

      This ensures proper cleanup of child resources after child terminates. "When a process exits, its parent is sent a CHLD signal by the kernel and the process becomes a zombie until the parent calls wait or waitpid. If you start another process in Perl using anything except fork, Perl takes care of reaping your zombied children, but if you use a raw fork, you're expected to clean up after yourself." (from Camel, 3rd ed.). I don't know really if this is needed on Win32, but it is supported, since "wait() and waitpid() can be passed a pseudo-process ID returned by fork(). These calls will properly wait for the termination of the pseudo-process and return its status." (from ActiveState Perl docs).

    • The else clause is the main server process. It blocks reading one line from STDIN and sends that to the newly forked socket connection.

    This is probably not the best example code for writing servers and clients. Have a look at the Perl Cookbook, chapter 17.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

      Ah. Thanks! I will hop back on to the Camel's back.