Re: NetServer::Generic -- welcome message ??



Babacio <babacio@xxxxxxx> wrote in comp.lang.perl.misc:

More precisely, I would appreciate any suggestion of a simple
way to run a forked/preforked tcp server, that would behave
like the following one. (Any comment on the code is welcome
as well).
use IO::Socket;

my $server = IO::Socket::INET->new(LocalPort => 9101,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 )
or die "$@\n";

while (my $client = $server->accept()) {
print $client "Welcome! Say 'quit' to leave.\n";

Why did you put this interaction outside the client loop?

while(1) {
my $x = <$client>;
last if $x =~ /^quit/i;

I'd put that last, so the "quit" interaction is logged as well.

$x =~ s/[\r|\n]//g;
print $client "You said $x!\n";
}
}

close($server);

To make a plain forked server, there isn't much to add. Just run
the client loop ("while ( 1 )...") in a forked process and exit after
the loop. Add a sigchld handler against zombies and error checking
and you're basically done. This is the loop (marginally tested)


$SIG{ CHLD} = 'IGNORE'; # good enough on most systems

while (my $client = $server->accept()) {
if ( my $pid = fork ) {
# parent does nothing here
} else {
print $client "Welcome! Say 'quit' to leave.\n";
die "can't fork" unless defined $pid;
while(1) {
my $x = <$client>;
$x =~ s/[\r|\n]//g;
print $client "(pid $$) You said $x!\n";
last if $x =~ /^quit/i;
}
exit;
}
}

If you need the ability for every client process to shut down the main
server, kill() the main process through an appropriate signal from the
child.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.



Relevant Pages

  • Re: multiple tcp server and client execution with close problem
    ... while loop to accept ... Why is theserverstill waiting for the client? ... The server is still waiting because there is another function calling ...
    (comp.unix.programmer)
  • Re: multiple tcp server and client execution with close problem
    ... Ignore the loop for accept. ... for acceptafter settting socket to blocking mode. ... Client side: ... execute main to run server 3 times: ...
    (comp.unix.programmer)
  • Re: Send large block of data failed
    ... It would help if you told us which exception you get. ... get all the data that was sent, you need to call stream.Read in a loop: ... Client send the actual buffer to the server. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DRb Crashing (on OS X Tiger)
    ... >The client crashes, ... >inside the server loop seems to resolve the issue. ... thing on Tiger both with your test code and my own DRb code. ...
    (comp.lang.ruby)
  • Re: What doesnt lend itself to OO?
    ... >> proxy and instructs the server to constuct the real object. ... rather than client code. ... If 'clock' is instantiated in the server, ... > for the server interface at the OOA level. ...
    (comp.object)