NetServer::Generic -- welcome message ??

B

Babacio

Hi,

I try to use NetServer::Generic. Let's look to the example of the doc.
(I copy/paste it).

my $server_cb = sub {
my ($s) = shift ;
print STDOUT "Echo server: type bye to quit, exit ",
"to kill the server.\n\n" ;
while (defined ($tmp = <STDIN>)) {
return if ($tmp =~ /^bye/i);
$s->quit() if ($tmp =~ /^exit/i);
print STDOUT "You said:>$tmp\n";
}
}
my ($foo) = new NetServer::Generic;
$foo->port(9000);
$foo->callback($server_cb);
$foo->mode("forking");
print "Starting server\n";
$foo->run();

The problem is that when connecting to the server (let's say with
telnet or Net::Telnet), the welcome message is displayed only after
the client sent something. Any hint ?
 
B

Babacio

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";
while(1) {
my $x = <$client>;
last if $x =~ /^quit/i;
$x =~ s/[\r|\n]//g;
print $client "You said $x!\n";
}
}

close($server);
 
A

Anno Siegel

Babacio said:
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
 
B

Babacio

(Anno Siegel)
Why did you put this interaction outside the client loop?

I don't want it to be printed out at each line, only at
connection. (A "welcome message").
I'd put that last, so the "quit" interaction is logged as well.

Yes, that may be more elegant.
$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)

Mmmh. OK. I may use this solution. Thanks a lot.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top