Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Answer: How do I get the local internet IP address?

( #521748=categorized answer: print w/ replies, xml ) Need Help??

Q&A > network programming > How do I get the local internet IP address? contributed by tirwhan

This question is usually asked to find out which IP address the local host uses to connect to a remote one. One important thing to realise in this context is that there is no such thing as a host's IP address. Network interfaces have IP addresses, not hosts, and a single network interface can have many (virtual) IP addresses. The operating system's routing subsystem decides which network interface and IP address to use to connect to a remote machine. If your machine only has one external network interface, and this interface only has one IP address then this IP address is commonly called the machine's address, but that is inaccurate. For example, if the machine is connected to a VPN via a virtual interface it will use this interface's IP address to connect to another machine on the VPN, not the external IP address.

Usually the best fail-safe way to find the "local Internet IP address" is to actually establish a network connection to the remote host and then find out which IP address is being used on the local side. For example:

use IO::Socket::INET; my $sock = IO::Socket::INET->new( PeerAddr=> "", PeerPort=> 80, Proto => "tcp"); my $localip = $sock->sockhost;

Comment on Answer: How do I get the local internet IP address?
Download Code
Replies are listed 'Best First'.
Re: Answer: How do I get the local internet IP address?
by ysth (Canon) on Jan 08, 2006 at 03:29 UTC
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2016-04-30 13:36 GMT
Find Nodes?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW

    Results (441 votes). Check out past polls.