How can i find my LAN IP Address using std.socket?

Johannes Pfau nospam at example.com
Tue Feb 4 14:23:37 PST 2014


Am Tue, 04 Feb 2014 16:19:08 +0000
schrieb "Stanislav Blinov" <stanislav.blinov at gmail.com>:

> On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh 
> wrote:
> 
> > This computer is on a network with dynamically assigned IP 
> > address (DHCP).
> > So shouldn't the 10.1.101.52 address have been reported?
> 
> Nope. In out-of-the-box simple network setups (i.e. home network 
> in the form PC/laptop -> router -> internet) the address 
> resolution won't go further than your hosts file, which in turn 
> will always give you back the loopback address (more 
> specifically, the address that is specified for the hostname in 
> aforementioned file).
> 
> That's why both I and Dicebot mentioned there isn't any real way 
> to query your local addresses without any live connection: only 
> when a socket has a connection within a particular network can 
> you tell your own IP address in that network. The address itself 
> would depend on the network setup, your local routing 
> configuration, etc. Your machine can have several network 
> adaptors (ethernet boards, Wi-Fi, etc.), each configured with 
> (numerous) routing setups, plus the routers they're connected to 
> have their own routing setups... This can go on and on.

As a last resort there are always OS specific APIs to iterate network
interfaces. For example, for linux:
http://man7.org/linux/man-pages/man3/getifaddrs.3.html

(Of course this doesn't tell you at all whether a local IP-address is
actually routable. And you probably have to figure out if you got
a link local / ULA IPv6 or a global one)


More information about the Digitalmars-d-learn mailing list