Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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!


In reply to Re^5: 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2021-06-22 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (105 votes). Check out past polls.

    Notices?