I'm trying to connect to some host, using invalid port, and i want to get timeout after X seconds. How to do that ?
My code:
$sock = new IO::Socket::INET(
PeerAddr => $_[0],
PeerPort => $_[1],
Proto => 'tcp',
Timeout => 2
);
If you check the code you'll see (I copied it from my Ubuntu 10.04) :
Apparently the timeout stuff is commented out so that expleins why it is ignored.
I found a post 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 :
Not very elegant, but if it works...
Let's verify with a slow server and impatient client :
if we run this:
So it ignores the 2 second timeout and runs for the full 5 seconds.
Now the other impatient client :
~
and running it :
Yep, this timeouts after 2 seconds as expected.