Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > NetServer::Generic -- welcome message ??

Reply
Thread Tools

NetServer::Generic -- welcome message ??

 
 
Babacio
Guest
Posts: n/a
 
      03-30-2006

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 ?
 
Reply With Quote
 
 
 
 
Babacio
Guest
Posts: n/a
 
      03-30-2006

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);

--
Ouah, ouah, ouah.
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      03-30-2006
Babacio <> 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.
 
Reply With Quote
 
Babacio
Guest
Posts: n/a
 
      03-30-2006
(Anno Siegel)

>> 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?


I don't want it to be printed out at each line, only at
connection. (A "welcome message").

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

>
> 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.

--
Ouah, ouah, ouah.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting Python from the terminal with no welcome message candide Python 2 03-01-2010 10:58 AM
Welcome screen not welcome mona@Ve.com Computer Support 4 12-14-2007 03:12 AM
Why "Welcome Message" I post come get posted as the reply to same post ? arnuld C++ 6 09-05-2007 05:24 PM
Thunderbird - message exceeds the Maximum Message Size? Gerry Firefox 0 02-18-2005 06:39 PM
mailing list welcome welcome msg in wiki suggestion Brian van den Broek Python 0 12-12-2004 09:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57