Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Getting net addy on localhost?

by rob_au (Abbot)
on Nov 27, 2001 at 09:40 UTC ( #127731=note: print w/replies, xml ) Need Help??


in reply to Getting net addy on localhost?

The reason that this code is returning 127.0.0.1 is that you are requesting the resolution of the name localhost, which should always be your loopback adapter address - If you want it to resolve your external IP address, you should pass the external hostname you have been assigned. If this is dynamic in nature then you may want to look at other solutions such as Net::Pcap which allow network interfaces to be iterated.

Update #1 - I did a bit of digging on a hunch and if you have a known external address that you can connect to, you could make a socket connection to it and then call perlfunc:getsockname on the socket handle (or sockname if you use IO::Socket) to return the packed address of this end of the socket. eg.

use Socket; $mysockaddr = getsockname(SOCK); ($port, $myaddr) = sockaddr_in($mysockaddr); printf "Connected from %s [%s]\n", scalar gethostbyaddr($myaddr, AF_INET), inet_ntoa($myaddr);

Update #2 - And the prize goes to converter for the link to IO::Interface in this post

 

Ooohhh, Rob no beer function well without!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://127731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2021-10-24 01:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (88 votes). Check out past polls.

    Notices?