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.
-
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 How to display code and escape characters
are good places to start.
|