- 论坛徽章:
- 7
|
仅适用tcp- #!/usr/bin/env perl -w
- use strict;
- use IO::Socket;
- my ($PeerAddr, $PeerPort, $TimeOut, $sock) = @ARGV;
- if (defined $TimeOut && ($TimeOut > 0)) {
- my $elapsed = 0;
- my $base = time;
- while ($elapsed < $TimeOut) {
- $sock = IO::Socket::INET->new( PeerAddr => $PeerAddr,
- PeerPort => $PeerPort,
- Proto => 'tcp',
- Timeout => $TimeOut);
-
- last if $sock;
- select(undef, undef, undef, 0.5);
- $elapsed += 0.5;
- $elapsed += (time - $base);
- }
- $sock or exit 1;
- close $sock;
- exit 0;
- }
- else {
- $sock = IO::Socket::INET->new( PeerAddr => $PeerAddr,
- Proto => 'tcp',
- PeerPort => $PeerPort) or exit 1;
- close $sock;
- exit 0;
- }
复制代码 |
|