in reply to Multihoming a non-blocking IO::Socket::INET

The addresses for MultiHomed or obtained using the following function in IO::Socket::INET:

sub _get_addr { my($sock,$addr_str, $multi) = @_; my @addr; if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) { (undef, undef, undef, undef, @addr) = gethostbyname($addr_str); } else { my $h = inet_aton($addr_str); push(@addr, $h) if defined $h; } @addr; }

To solve your problem, get the addresses in a similar matter, create a non-blocking socket to each address, and wait for one to connect using IO::Select.

use IO::Select (); use IO::Socket qw( AF_INET SOCK_STREAM ); sub get_addrs { my ($domain) = @_; my (undef, undef, undef, undef, @addr) = gethostbyname($domain); return @addr; } sub aggressive_connect { my ($domain, $port, $timeout) = @_ $timeout ||= 0; my @socks; foreach my $packed_addr (get_addrs($domain)) { my $sock = IO::Socket->new( Domain => AF_INET, # ip (as opposed to unix) Type => SOCK_STREAM, # tcp ); $sock->blocking(0); $sock->connect($port, $packed_addr) or next; push(@socks, $sock); } return (IO::Select->new(@socks)->can_write($timeout))[0]; ) my $sock = aggressive_connect('alistapart.com', 80, 30) or die("Unable to connect\n"); # $sock->blocking(1); # Revert to blocking if so desired.

Untested.