I have a (from what I can tell) perfectly working Linux setup (Ubuntu 8.04) where all tools (nslookup, curl, wget, firefox, etc) are able to resolve addresses. Yet, the following code fails:
$s = new IO::Socket::INET( PeerAddr => 'stackoverflow.com', PeerPort => 80, Proto => 'tcp', ); die 'Error: $!\n' unless $s;
I verified the following things:
-
Perl is able to resolve addresses with gethostbyname (ie the code below works):
my $ret = gethostbyname('stackoverflow.com'); print inet_ntoa($ret); -
The original source code works under Windows
- This is how it supposed to work (ie. it should resolve hostnames), since LWP tries to use this behavior (in fact I stumbled uppon the problem by trying to debug why LWP wasn’t working for me)
- Running the script doesn’t emit DNS requests (so it doesn’t even try to resolve the name). Verified with Wireshark
From a quick look, the following code from IO::Socket::INET
suggests (if you look at the caller of this code) the work-around of adding
MultiHomed => 1,to your code.Without that work-around, the above code appears to try to call
inet_aton('hostname.com')using the inet_aton() from Socket.pm. That works for me in both Win32 and Unix, so I guess that is where the breakage lies for you.See Socket.xs for the source code of inet_aton:
It appears that the Perl gethostbyname() works better than the C gethostbyname() for you.