Try this:
#! perl -slw use strict; use Time::HiRes qw[ time ]; binmode STDIN; my $buf = ''; while( 1 ) { sysread( STDIN, $buf, 1 ); my $end = time() + 7; sleep 0.1 while time() < $end; sysread( STDIN, $buf, 1024, length( $buf ) ); printf "Got: '%f'\n", unpack 'd', $buf; }
The idea is that instead of trying to interrupt or timeout a read after 7 seconds; you read 1 byte; blocking until it arrives; you then wait 7 seconds and then read the rest of whatever has arrived in the interim.
When combined with this:
#! perl -sw use strict; use Time::HiRes qw[ time ]; binmode STDOUT; while( 1 ) { my $packet = pack 'd', time; syswrite( STDOUT, $packet ); sleep 30; }
It produces this:
C:\test>sender | listener Got: '1423634772.508475' Got: '1423634802.506555' Got: '1423634832.504635' Terminating on signal SIGINT(2) Terminating on signal SIGINT(2)
In reply to Re: How I unconditionally wait for data in 7 seconds?
by BrowserUk
in thread How I unconditionally wait for data in 7 seconds?
by sebastiannielsen2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |