Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 04, 2014 How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
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) |
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | 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)
You can have lot of different local IP addresses on a single machine so question can't be answered properly. However you may use in `Socket.hostName` and resolve it via DNS to find IP machine itself currently consuders as its main pulbic IP.
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | 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)
Create a connection to another LAN machine with a known address (e.g. gateway or router), then use Socket's localAddress property to get your IP. You cannot really do that before establishing a connection, as Dicebot already mentioned.
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov wrote:
> Create a connection to another LAN machine with a known address (e.g. gateway or router), then use Socket's localAddress property to get your IP.
Worth noting that this solution is not reliable in general either because your server can possibly have complicated routing configurations that will make, for example, LAN destination packets go via different network interface than WAN destination ones.
It is probably better to tell what high-level problem you are trying to solve to find most useful compromise.
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Tuesday, 4 February 2014 at 13:13:07 UTC, Dicebot 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)
>
> You can have lot of different local IP addresses on a single machine so question can't be answered properly. However you may use in `Socket.hostName` and resolve it via DNS to find IP machine itself currently consuders as its main pulbic IP.
This works great thanks. I just need any (working) local IP address so getting more then one is not an issue for me.
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Tuesday, 4 February 2014 at 13:21:54 UTC, Stanislav Blinov 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)
>
> Create a connection to another LAN machine with a known address (e.g. gateway or router), then use Socket's localAddress property to get your IP. You cannot really do that before establishing a connection, as Dicebot already mentioned.
Problem is that i don't know in what local network the server will be running, so this is unfortunatly not an option for me.
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | On Tuesday, 4 February 2014 at 13:31:27 UTC, TheFlyingFiddle wrote:
> Problem is that i don't know in what local network the server will be running, so this is unfortunatly not an option for me.
But if that's the case, the hostname solution may as well just give you your loopback address. :)
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | 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.
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vladimir Panteleev | 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?
|
February 04, 2014 Re: How can i find my LAN IP Address using std.socket? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | On Tuesday, 4 February 2014 at 16:02:33 UTC, Craig Dillabaugh wrote:
> 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?
It results in all addresses you hostname resolvs to. On all desktop linux machines /etc/hosts is configured to resolve hostname to "localhost" by default. On servers it usually resolves to externally accessible one.
|
Copyright © 1999-2021 by the D Language Foundation