my $timeout = time+$X; # timeout if we don't see anything in $X seconds 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 something, # 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"; }