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.

In reply to Re^10: IO::Socket tutorial by BernieC
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.