As you may have gathered, the Timeout IO::Socket::INET option is expected to apply only to connect and accept -- and some people doubt whether even that works on Win32... but more on that, below.
For sysread and syswrite I know of three ways to introduce a time-out:
use alarm and a signal handler. I've seen an eval wrapped around a read or write operation, with a signal handler that dies. A quick experiment suggests this is not necessary, an ALRM signal causes sysread to return with an EINTR "soft" error. However, alarm doesn't appear to work on Win32 -- I cannot find chapter and verse on this limitation, but I can say when I tried it (perl 5.10.0, ActivePerl Build 1002 283697) it did not work.
socket options SO_RCVTIMEO and SO_SNDTIMEO seem to offer exactly what's required. Unfortunately the POSIX (1003.1, 2004) specification says "not all implementations allow this option to be set", which isn't encouraging -- life is complicated enough, already. When I tried it, Linux (2.6.27.7-53) rejected the attempt to $sock->sockopt(SO_RCVTIMEO, 10) :-(
the final way is to use select, which may seem to be hard work, but is actually straighforward and has the advantage of working ! The following will read from an open socket $sock until it is closed by the far end, or nothing arrives for ten seconds, or some error occurs:
and something similar should work for syswrite. Note that it is not necessary to set the socket non-blocking in order to use select, or for sysread to return with a partial result -- but it won't hurt to set the socket non-blocking.my $select = IO::Select->new($sock) ; my $buffer = '' ; my $rc ; while (1) { $! = undef ; # no error, yet if ($select->can_read(10)) { $rc = sysread($sock, $buffer, 64*1024, length($buffer)); next if $rc ; # continue if not error and not "eof" last if defined($rc) ; # exit if "eof" } else { $rc = $! ? undef : 1 ; # $rc == undef if error, == 1 if time o +ut last if $rc ; # exit if time out } ; redo if $! == EAGAIN ; # Not really expected, even with non-bl +ocking redo if $! == EINTR ; # Signals ! last ; # exit on "hard" error } ; # Now: $rc == 0 => no error, == 1 => timed out, == undef => error
At some time in the past IO::Socket may have used alarm to implement time-out for connect/new and for accept. However, current implementation (v1.31) uses IO::Select, and works happily on Win32. (Looking at the ChangeLog, this appears to have been the case since v1.15, Sun 19 Jan 1997.)
In reply to Re: what does timeout mean in IO::Socket::INET ?
by gone2015
in thread what does timeout mean in IO::Socket::INET ?
by sunshine_august
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |