NetServer::Generic -- welcome message ??

Discussion in 'Perl Misc' started by Babacio, Mar 30, 2006.

  1. Babacio

    Babacio Guest

    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 ?
    Babacio, Mar 30, 2006
    #1
    1. Advertising

  2. Babacio

    Babacio Guest

    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.
    Babacio, Mar 30, 2006
    #2
    1. Advertising

  3. Babacio

    Anno Siegel Guest

    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.
    Anno Siegel, Mar 30, 2006
    #3
  4. Babacio

    Babacio Guest

    (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.
    Babacio, Mar 30, 2006
    #4
    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. Murat Tasan
    Replies:
    1
    Views:
    8,025
    Chaitanya
    Feb 3, 2009
  2. Replies:
    2
    Views:
    423
  3. Brian van den Broek

    mailing list welcome welcome msg in wiki suggestion

    Brian van den Broek, Dec 12, 2004, in forum: Python
    Replies:
    0
    Views:
    580
    Brian van den Broek
    Dec 12, 2004
  4. arnuld
    Replies:
    6
    Views:
    590
    arnuld
    Sep 5, 2007
  5. candide
    Replies:
    2
    Views:
    385
    candide
    Mar 1, 2010
Loading...

Share This Page