Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^5: IO::Socket tutorial

by haukex (Archbishop)
on Feb 20, 2020 at 00:31 UTC ( [id://11113211]=note: print w/replies, xml ) Need Help??


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

I'm thinking of sticking with IO::Select and it looks very easy. ... then I can do a can_read with a timeout, and if it doesn't time out I can use ->getlines to suck up what was sent

No, unfortunately it's not that easy. The reason is that can_read will fire even if there's only a single byte waiting to be read, and readline and friends will still hang because it's looking for a full line, which the server might not have sent.

srv.pl:

use warnings; use strict; use IO::Select; use IO::Socket; my $sock = IO::Socket::INET->new( Listen=>1, ReusePort=>1, LocalAddr=>'127.0.0.1', LocalPort=>1235 ) or die "sock: $@"; my $sel = IO::Select->new($sock); while ( my @ready = $sel->can_read ) { for my $fh (@ready) { if ($fh == $sock) { my $cli = $sock->accept; print "New client\n"; $sel->add($cli); syswrite $cli, "x"; } } }

cli.pl:

use warnings; use strict; use Data::Dumper; use IO::Select; use IO::Socket; my $sock = IO::Socket::INET->new( PeerAddr=>'127.0.0.1', PeerPort=>1235 ) or die "sock: $@"; my $sel = IO::Select->new($sock); while ( my @ready = $sel->can_read(1) ) { for my $fh (@ready) { print "Attempting to read...\n"; # all of these hang! #print Dumper($fh->getlines); #print Dumper($fh->getline); print Dumper(scalar <$fh>); } }

Note these aren't complete examples as they are oversimplified and lack error handling. Anyway, sure, you could implement your own routine to read the socket byte-by-byte until you've got a full line, but that's a wheel that's been re-implemented a million times (I've done it several times myself). Again: I strongly recommend you use a library that already provides this functionality!

Replies are listed 'Best First'.
Re^6: IO::Socket tutorial
by BernieC (Pilgrim) on Feb 20, 2020 at 12:22 UTC
    Thanks for the correction. I wondered how can_read and getline{s} interacted {answer: not at all}. I hear you about not reinventing the wheel and I'll look again at 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?

    I confess that I'm a bit surprised that IO doesn't provide that facility. I could see it implementing a version of getline{} that took a timeout and gave you either the line you asked for or an error. I expect that {as you said} that functionality keeps getting reinvented, so it is a bit odd that over the years no one has added that to the IO family.

    Many many years ago I wrote a server in Perl and I had to do just as you said: the low-level sub had to read a byte at a time then assemble the response and return a complete line. I only remember it being kinda clunky but worked. And I wish I still had the code I wrote to do that :)

      <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:

      mojo_serv.pl:

      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 => '127.0.0.1', port => 3000, reuse => 1); $server->start; $server->reactor->start unless $server->reactor->is_running;

      mojo_cli.pl:

      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 => '127.0.0.1', port => 3000); $client->reactor->start unless $client->reactor->is_running;

      Here's some semi-tested client code that reads what it can. then does the inner loop for each whole line.

      # cli.pl: use strict; use warnings; use IO::Select; use IO::Socket; my $sock = IO::Socket::INET->new('127.0.0.1:1235') or die "sock: $@"; my $sel = IO::Select->new($sock); my $buffer = ''; while(@1 = $sel->can_read(2) and sysread $sock, $buffer, 4096, length +$buffer) { while( $buffer =~ s/^.*\n// ) { my $line = $&; print "got: ", $line; # or whatever processing you want here... } } die @1 ? "socket closed" : "timeout";

      If you change the read size to 1 you get the read-a-byte-at-a-time behavior.

        That last bit is perfect, and pretty much exactly what I did to do a read loop over an ssh connection. And a good thing: ::Linereader looked like just the right thing, but:
        FERREIRA/Mojo-IOLoop-LineReader-0.3.tar.gz C:\STRAWB~1\c\bin\gmake.exe test -- NOT OK //hint// to see the cpan-testers results for installing this module, t +ry: reports FERREIRA/Mojo-IOLoop-LineReader-0.3.tar.gz Stopping: 'install' failed for 'Mojo::IOLoop::LineReader'. Failed during this command: FERREIRA/Mojo-IOLoop-LineReader-0.3.tar.gz : make_test NO
        I must have bad luck. On another thread I discovered that SVG::TT::Graph::XY wouldn't install, either. Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found