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.


In reply to Re: Multihoming a non-blocking IO::Socket::INET by ikegami
in thread Multihoming a non-blocking IO::Socket::INET by vancetech

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.