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;

In reply to Re: IO::Socket tutorial by haukex
in thread IO::Socket tutorial by BernieC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.