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

Craig Dillabaugh cdillaba at cg.scs.carleton.ca
Tue Feb 4 08:02:32 PST 2014


On Tuesday, 4 February 2014 at 15:48:50 UTC, Vladimir Panteleev 
wrote:
> On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle 
> wrote:
>> I'm trying to find my own ip address using std.socket with 
>> little success. How would i go about doing this? (It should be 
>> a AddressFamily.INET socket)
>
> This program will print all of your computer's IP addresses:
>
> import std.socket;
> import std.stdio;
>
> void main()
> {
> 	foreach (addr; getAddress(Socket.hostName))
> 		writeln(addr.toAddrString());
> }
>
> This includes IPv6 addresses. You can filter the address family 
> by checking addr's addressFamily property.

I am a bit lost in anything networking related, so I ran this on 
my machine just for fun and it printed:

127.0.0.1
127.0.0.1
127.0.0.1

which based on my understanding is the local loopback address (I 
am not completely, 100% ignorant).

However if I run /sbin/ifconfig I get:

enp7s0    Link encap:Ethernet  HWaddr 50:E5:49:9B:29:49
           inet addr:10.1.101.52  Bcast:10.1.101.255  
Mask:255.255.255.0
           inet6 addr: fe80::52e5:49ff:fe9b:2949/64 Scope:Link

lo        Link encap:Local Loopback
           inet addr:127.0.0.1  Mask:255.0.0.0
           inet6 addr: ::1/128 Scope:Host

This computer is on a network with dynamically assigned IP 
address (DHCP).
So shouldn't the 10.1.101.52 address have been reported?


More information about the Digitalmars-d-learn mailing list