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:

  1. 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.

  2. 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) :-(

  3. 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:

    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
    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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.