in reply to IO::Socket tutorial

Here's a complete example with Mojo::IOLoop and Mojo::IOLoop::Stream::Role::LineBuffer:

mojo_serv.pl:

#!/usr/bin/env perl use warnings; use strict; use Mojo::Util qw/dumper/; use Mojo::IOLoop; use Mojo::IOLoop::Stream::Role::LineBuffer; Mojo::IOLoop->server( { address => '127.0.0.1', port => 3000, reuse => 1 } => sub { my ($loop, $stream) = @_; my $peer = $stream->handle->peerhost.":".$stream->handle->peer +port; print "$peer: Connect\n"; $stream->timeout(30); $stream = $stream->with_roles('+LineBuffer')->watch_lines; $stream->on(read_line => sub { my ($strm, $line, $sep) = @_; print "$peer: Got line ".dumper($line); }); $stream->on(timeout => sub { print "$peer: Timeout\n" }); $stream->on(close => sub { print "$peer: Closed\n" }); $stream->on(error => sub { warn "$peer: Error: $_[1]" }); $stream->write("Hello, client from $peer.\n"); }); print "Starting server...\n"; Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

mojo_cli.pl:

#!/usr/bin/env perl use warnings; use strict; use Mojo::Util qw/dumper/; use Mojo::IOLoop; use Mojo::IOLoop::Stream::Role::LineBuffer; Mojo::IOLoop->client( { address => '127.0.0.1', port => 3000 } => sub { my ($loop, $err, $stream) = @_; if (defined $err) { warn "Error: $err"; return } print "Connect\n"; $stream->timeout(10); $stream = $stream->with_roles('+LineBuffer')->watch_lines; $stream->on(read_line => sub { my ($strm, $line, $sep) = @_; print "Got line ".dumper($line); }); $stream->on(timeout => sub { print "Timeout\n" }); $stream->on(close => sub { print "Closed\n" }); $stream->on(error => sub { warn "Error: $_[1]" }); $stream->write("Hello, server, I am a client.\n"); }); print "Starting client...\n"; Mojo::IOLoop->start unless Mojo::IOLoop->is_running;