9.3. Simple Echo Server

The Simple Echo Server lets you telnet to it and echos back every word you type.

Example 9-3. examples/server/simple_echo_server.pl

#!/usr/bin/perl
use strict;
use warnings;

use lib 'lib';
use SimpleEchoServer;

SimpleEchoServer->run(port => 8000);

Example 9-4. examples/server/lib/SimpleEchoServer.pm

package SimpleEchoServer;
use warnings;
use strict;

use base 'Net::Server';
my $EOL   = "\r\n";

sub process_request {
    my $self = shift;
    while( my $line = <STDIN> ) {
        $line =~ s/\r?\n$//;
        print qq(You said "$line"$EOL);
        last if $line eq "bye";
    }
}

1;

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed