$\=$/; use IO::Socket::INET; my @hosts = qw(pantagruel gargantua foo bar quux); my (%active,%sockets); my $maxconn = 30; # or as much as your OS permits ;-) my $timeout = 20; sub setup_sock { my $fh = eval { IO::Socket::INET->new( PeerAddr => "$_[0]:23", Blocking => 0, ) }; unless ($fh) { print "$_[0] not reachable"; return; } $active{$_[0]} = $fh; $sockets{fileno($fh)} = $_[0]; # print "$_[0] fileno ",fileno($fh); } sub close_sock { my $fileno = shift; my $host = $sockets{$fileno}; $active{$host}->close; delete $active{$host}; delete $sockets{$fileno}; } sub set_bits { my $rin; for (keys %active) { vec($rin, fileno($active{$_}),1) = 1; } $rin; } while (@hosts || keys %active) { # $c++; print "pass $c"; setup_sock(shift @hosts) while ($maxconn > keys %active and @hosts); my $rin = set_bits; my $rout; select($rout=$rin,undef,undef,$timeout); # if there has been a timeout, there has been no response, # and $rout has no bit set. mark those hosts as unreachable if ($rout eq "\0") { for(0..unpack"B*",$rin) { if (vec $rin,$_,1) { print "no answer from $sockets{$_} (fileno $_)"; close_sock($_); } } } for(0..unpack"B*",$rout) { if (vec $rout,$_,1) { print "$sockets{$_} ($_) is alive"; close_sock($_); } } }