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

Re^3: use sysread to read non-blocking filehandle

by roboticus (Chancellor)
on Nov 04, 2014 at 13:18 UTC ( #1106028=note: print w/replies, xml ) Need Help??


in reply to Re^2: use sysread to read non-blocking filehandle
in thread use sysread to read non-blocking filehandle

x12345:

Not exactly--it can also mean "I haven't received a response yet". So you're probably terminating the loop before the other side ever had a chance to reply. (Considering the timing, you may be exiting before the other computer even received the request.)

Update: It can also mean "The network is really busy, so the next packet hasn't arrived yet." That could be a problem if you've received only part of your response.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re^3: use sysread to read non-blocking filehandle

Replies are listed 'Best First'.
Re^4: use sysread to read non-blocking filehandle
by x12345 (Novice) on Nov 04, 2014 at 13:36 UTC
    Could you show me how to change my code to get the results?

      x12345:

      This is a bit oversimplified, as there's insufficient error checking and such. But I'd suggest doing something like "terminate the loop if I haven't received anything for X seconds" (where you'd have to choose X to balance between performance, and the possibility of a false failure). You could start with something like this:

      my $timeout = time+$X; # timeout if we don't see anything in $X secon +ds my $ttl_bytes_rcvd=0; # haven't received anything yet while (time < $timeout) { ... read the data ... if ($bytes_read>0) { $timeout = time+$X; # Reset timeout if we actually saw some data $ttl_bytes_rcvd += $bytes_read; } # don't just hammer the CPU, give other processes a chance to do so +mething, # as well as allowing a bit of time for more data to arrive sleep 0.5; } if ($ttl_bytes_rcvd) { # process successful response ... } else { die "Error! No data received!\n"; }

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      You could check whether you closed your session and don't expect any more data. Maybe eof could help there too.

      Using these two criteria, you should be able to determine whether there was just not anything read, or whether the other end has closed the connection.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2020-11-24 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?