Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^7: IO::Socket tutorial (updated x2!)

by haukex (Bishop)
on Feb 20, 2020 at 14:02 UTC ( #11113246=note: print w/replies, xml ) Need Help??

in reply to Re^6: IO::Socket tutorial
in thread IO::Socket tutorial

<update2> Please disregard the following code example, I was pointed to Mojo::IOLoop::Stream::Role::LineBuffer which makes the implementation much nicer - many thanks to the fine people on #mojo on freenode! See this example instead. </update2>

<update> Note that this uses the slightly lower-level API instead of the higher-level one shown at the top of Mojo::IOLoop. However, I don't yet see a easy way to use one of the existing readline modules with that interface (Mojo::IOLoop::LineReader or MojoX::LineStream, the latter has a bug and doesn't support changing the input record separator). I may have another update in a little while, we'll see. </update>

POE.. it seems awfully complicated for something as simple as what I need {basically *nothing* more than an unblocking read} Is there a less complicated package that provides the simple functionality I need?

Yes, I agree POE has an "interesting" interface, although once you get into it, it works well. As I mentioned, Mojolicious includes an event loop whose interface I find nicer. There's also Mojo::IOLoop::LineReader that handles the buffering and splitting of read events into lines:

use warnings; use strict; use Data::Dump qw/dd pp/; use Mojo::IOLoop::Server; use Mojo::IOLoop::LineReader; my $server = Mojo::IOLoop::Server->new; $server->on(accept => sub { my ($serv, $handle) = @_; my $peer = $handle->peerhost.":".$handle->peerport; print $peer,": Connect\n"; my $stream = Mojo::IOLoop::LineReader->new($handle); $stream->timeout(30); $stream->on(readln => sub { my ($strm, $line) = @_; print $peer,": Got line ",pp($line),"\n"; }); $stream->on(close => sub { print $peer,": Closed\n"; $stream->stop; $stream = undef; # free reference }); $stream->on(error => sub { my ($strm, $err) = @_; warn "$peer: Error: $err"; }); $stream->start; $stream->write("Hello, client from $peer.\n"); }); $server->listen(address => '', port => 3000, reuse => 1); $server->start; $server->reactor->start unless $server->reactor->is_running;

use warnings; use strict; use Data::Dump qw/dd pp/; use Mojo::IOLoop::Client; use Mojo::IOLoop::LineReader; my $client = Mojo::IOLoop::Client->new; $client->on(connect => sub { my ($cli, $handle) = @_; print "Connect\n"; my $stream = Mojo::IOLoop::LineReader->new($handle); $stream->timeout(10); $stream->on(readln => sub { my ($strm, $line) = @_; print "Got line ",pp($line),"\n"; }); $stream->on(close => sub { print "Closed\n"; $stream->stop; $stream = undef; # free reference }); $stream->on(error => sub { my ($strm, $err) = @_; warn "Error: $err"; }); $stream->start; $stream->write("Hello, server, I am a client.\n"); }); $client->on(error => sub { my ($cli, $err) = @_; warn "Error: $err"; }); $client->connect(address => '', port => 3000); $client->reactor->start unless $client->reactor->is_running;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11113246]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2021-04-11 11:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found