Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: DNS Resolution4

by hdp (Beadle)
on Apr 25, 2001 at 03:45 UTC ( [id://75319] : note . print w/replies, xml ) Need Help??


in reply to DNS Resolution4

Your code doesn't work because you're passing a hostname to inet_ntoa; it expects an ip address packed into four characters.

I think you probably misread this example:

($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle)); $peer_host = gethostbyaddr($iaddr, AF_INET); $peer_addr = inet_ntoa($iaddr);
Note that $iaddr, not the return value of gethostbyaddr, is passed to inet_ntoa. What you want can be reduced to a single line:
(gethostbyaddr(gethostbyname($ipaddr), AF_INET))[0]
hdp.

p.s. chomp $ipaddr or it'll continue to fail, but in a new and interesting way!