in reply to Timeout problem using IO::Socket

Here's a version that times out on opening failures. I'm still not clear where your actual concern is, since (on my machine) opening the socket times out after 5 seconds or so, in any case. By the way, if I ping 127.0.0.x on my machines, I get a reply, and connecting to a non-existant socket returns an immediate error. I had to attempt to connect to an IP address that I *knew* didn't exist to get the timeout to occur, and set a fairly short timeout.

The reason we (I) have to use a variable and not $@ (see perldoc -f eval), is because apparently IO::Socket sets up it's own eval() statement, and $@ never gets set. I had a version of code where the die() returned a string, and I found that although the die() was executing (determined by a print statement), $@ was not being returned correctly. Perhaps someone more enlightened than I can explain this (it's at the bottom of the node). I'm using 5.005-63 at work, and can't get to my machine at home to try it on 5.005_03.
#!/usr/local/bin/perl -w use strict; use IO::Socket; { my $timeout = 0; my $sock = undef; $SIG{ALRM} = sub {$timeout = 1; die}; eval { alarm (2); $sock = new IO::Socket::INET (PeerAddr => '10.1.1.18', PeerPort => 29, ); alarm (0); }; die "Can't open socket: timeout=$timeout\n" if ($timeout or !$sock) +; print "I would print to the socket now, if I knew what I was connec +ted to\n"; close ($sock); }

#!/usr/local/bin/perl -w use strict; use IO::Socket; { my $sock = undef; $SIG{ALRM} = sub {die "GOT TIRED OF WAITING"}; eval { alarm (2); $sock = new IO::Socket::INET (PeerAddr => '10.0.0.18', PeerPort => 29, ); alarm (0); }; die "Can't open socket: $@\n" if (!$sock); print "I would print to the socket now, if I knew what I was connec +ted to\n"; close ($sock); }
--Chris

e-mail jcwren