Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^7: IO::Socket tutorial

by tybalt89 (Monsignor)
on Feb 20, 2020 at 14:42 UTC ( [id://11113247]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^8: IO::Socket tutorial
by BernieC (Pilgrim) on Feb 20, 2020 at 20:13 UTC
    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!
      And a good thing: ::Linereader looked like just the right thing, but: ... Stopping: 'install' failed for 'Mojo::IOLoop::LineReader'.

      Right, which is why I updated my node to point to a better way, that should work on Windows (and a fair bit of time before you posted this, I want to add).

        I have, at last, gotten it all working. It took some tweaking and wasn't all that easy to figure out. My environment is that I do two types of interactions on my IO::Socket::INET. One is a simple command->{one line} response. The other is a multi-line response, but the same basic structure: command->{one line} response->{multiline data ended by a line with just '." on it. Here's the working code:
        $server = IO::Socket::INET->new(...) ; abort("Can't connect to news server: $!") unless $server ; $socket = IO::Select->new($server) ; my $linebuffer; # send a command to the server, 1,2, or 3 are success responses. ## This really can't hang, so we can just go for it [no timeout] sub command { my $cmd = "$_[0]\r\n" ; print $server $cmd ; $linebuffer = "" ; my $resp = getline() ; return undef if $resp !~ /^[123]/ ; return $resp ; } # return a multi-line response from the server ## We return it a line at a time with timeouts if the server barfs sub multi { my $cmd = $_[0] ; if ($cmd) { return (command($cmd)) ; } return getline() ; } sub getline { my @readstatus ; if ($linebuffer =~ s/(^.*?\n)// ) { return $1 ; } while(@readstatus = $socket->can_read(2) and sysread $server, $linebuffer, 4096, length $linebuffer) { next unless $linebuffer =~ s/(^.*?\n)// ; return $1 ; } # if you get here, something went wrong! warn(@readstatus ? "socket closed\n" : "timeout\n") ; return undef ; }
        The main program that uses this looks like:
        die "command error\n" unless multi("command") ; while (1) { my $line = multi() ; last if $line =~ /^./ ; {process the line} }
        One other trickiness is that I couldn't get the timeout to work cleanly -- if something went wrong I had a hard time figuring out whether it was recoverable or not. So, I embedded the entire main program in a child fork and the child fork exits with a non-zero status if timeout happens or the connection is lost or if anything else happens. The parent then re-forks and starts the child over again from the beginning. If the child returns with a 0 status the parent just exits.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found