Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you check the code you'll see (I copied it from my Ubuntu 10.04) :</p> <pre><code> my $timeout = ${*$sock}{'io_socket_timeout'}; # my $before = time() if $timeout; undef $@; if ($sock-&gt;connect(pack_sockaddr_in($rport, $raddr))) { # ${*$sock}{'io_socket_timeout'} = $timeout; return $sock; } return _error($sock, $!, $@ || "Timeout") unless @raddr; # if ($timeout) { # my $new_timeout = $timeout - (time() - $before); # return _error($sock, # (exists(&amp;Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL), # "Timeout") if $new_timeout &lt;= 0; # ${*$sock}{'io_socket_timeout'} = $new_timeout; # } </code></pre> <p>Apparently the timeout stuff is commented out so that expleins why it is ignored.</p> <p>I found a <a href="http://www.webmasterworld.com/forum13/3140.htm" rel="noreferrer">post</a> dating from 2003 where this was discussed. One suggestion (at the bottom) was to open the socket in an eval block which gets terminated by an alarm signal :</p> <pre><code>eval { local $SIG{ALRM} = sub { die 'Timed Out'; }; alarm 3; my $sock = IO::Socket::INET-&gt;new( PeerAddr =&gt; inet_ntoa( gethostbyname($host) ), PeerPort =&gt; 'whois', Proto =&gt; 'tcp', ## timeout =&gt; , ); $sock-&gt;autoflush; print $sock "$qry\015\012"; undef $/; $data = &lt;$sock&gt;; $/ = "\n"; alarm 0; }; alarm 0; # race condition protection return "Error: timeout." if ( $@ &amp;&amp; $@ =~ /Timed Out/ ); return "Error: Eval corrupted: $@" if $@; </code></pre> <p>Not very elegant, but if it works...</p> <p>Let's verify with a slow server and impatient client :</p> <pre><code># Impatient Client use IO::Socket::INET; $sock = new IO::Socket::INET( PeerAddr =&gt; "localhost", PeerPort =&gt; "10007", Proto =&gt; 'tcp', Timeout =&gt; 2, ); print &lt;$sock&gt;; close($sock); # SlowServer use IO::Socket::INET; $sock = new IO::Socket::INET( LocalAddr =&gt; "localhost", LocalPort =&gt; "10007", Proto =&gt; 'tcp', Listen =&gt; 1, Reuse =&gt; 1, ); $newsock = $sock-&gt;accept(); sleep 5; #while (&lt;$newsock&gt;) { # print $_; #} print $newsock "Some Stuff"; close($newsock); close($sock); </code></pre> <p>if we run this:</p> <pre><code>pti@pti-laptop:~/playpen$ perl server.pl&amp; [1] 9130 pti@pti-laptop:~/playpen$ time perl test.pl Some Stuff[1]+ Done perl server.pl real 0m5.039s user 0m0.050s sys 0m0.030s </code></pre> <p>So it ignores the 2 second timeout and runs for the full 5 seconds.</p> <p>Now the other impatient client :</p> <pre><code>use IO::Socket::INET; eval { local $SIG{ALRM} = sub { die 'Timed Out'; }; alarm 2; $sock = new IO::Socket::INET( PeerAddr =&gt; "localhost", PeerPort =&gt; "10007", Proto =&gt; 'tcp', Timeout =&gt; 2, ); print &lt;$sock&gt;; close($sock); alarm 0; }; alarm 0; # race condition protection print "Error: timeout." if ( $@ &amp;&amp; $@ =~ /Timed Out/ ); print "Error: Eval corrupted: $@" if $@; </code></pre> <p>~ </p> <p>and running it :</p> <pre><code>pti@pti-laptop:~/playpen$ perl server.pl&amp; [1] 9175 pti@pti-laptop:~/playpen$ time perl test2.pl Error: timeout.Error: Eval corrupted: Timed Out at test2.pl line 3. real 0m2.040s user 0m0.020s sys 0m0.010s </code></pre> <p>Yep, this timeouts after 2 seconds as expected.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload