smack has asked for the wisdom of the Perl Monks concerning the following question:
use IO::Socket; use IO::Scalar; my $port = 80; print "Please enter an IP: "; my $ip = <STDIN>; print "\nTrying $ip"; my $TIMEOUT = 3; # seconds eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $TIMEOUT; &CheckHost($ip, $port); alarm 0; }; if ($@) { if($@ eq "alarm\n") { die "Connection Timed out.\n"; } else { print "Socket Closed.\n"; } } sub CheckHost() { ($hostip, $hostport) = ($_[0], $_[1]); my $sock = new IO::Socket::INET ( PeerAddr => "$hostip", PeerPort => "$hostport", Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; chomp $hostport; print "Port $hostport is active on $hostip\n"; close($sock); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Windows socket timout code
by mifflin (Curate) on Sep 03, 2004 at 19:07 UTC | |
by smack (Beadle) on Sep 04, 2004 at 12:08 UTC | |
by smack (Beadle) on Sep 04, 2004 at 12:13 UTC | |
by smack (Beadle) on Sep 04, 2004 at 12:12 UTC |