Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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=> "example.com", PeerPort=> 80, Proto => "tcp"); my $localip = $sock->sockhost;

Comment on Answer: How do I get the local internet IP address?
Download Code
Re: Answer: How do I get the local internet IP address?
by ysth (Canon) on Jan 08, 2006 at 03:29 UTC
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-12-22 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (119 votes), past polls