0

I need a program to determine its LAN IP, the one other LAN clients would use to identify it. The .Net code I have found returns all IP addresses, including loopback and VMWare interfaces, so I get a list of Class C IPs.

I have already read through:

Find correct Ip address returned by Dns.GetHostEntry

Getting server ip using Dns.GetHostEntry in c#

Get IP address from hostname in LAN

Dns.GetHostEntry returns multiple IP addresses

Example code that returns all adapters, including the "right one", along with VMWare adapters, etc.

public string GetLanIP()
{
    IPHostEntry host;
    string localIP = "?";
    host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            localIP = ip.ToString();
        }
    }
    return localIP;
}

I have also tried creating a UDP client to the internet server in hopes I could find which LAN adapter was associated with it, so I could then find that adapter's IP Address, but no luck.

Can anyone advise on how to find the correct LAN IP that other computers on the same LAN would use to connect to it?

jbbarnes77
  • 119
  • 2
  • 10
  • If the computer has multiple NICs connected to multiple subnets, you may only be able to determine the LAN address it will use based on the destination IP address. If it is setup with some type of load balancing, even that may not be sufficient - the lower level network drivers will switch IP addresses dynamically. The only real answer would be to ask the computer you care about to tell you. And again, even then it may change between connections. – NetMage Jan 11 '19 at 21:40
  • I recommend this approach: https://stackoverflow.com/a/27376368/2557128 – NetMage Jan 11 '19 at 21:46
  • The approach using endpoints was exactly how I was trying to approach it and that link worked perfectly. If you want to make it an answer, I will select it. – jbbarnes77 Jan 13 '19 at 18:27
  • Possible duplicate of [Get local IP address](https://stackoverflow.com/questions/6803073/get-local-ip-address) – Andrew Morton Jan 14 '19 at 20:23

1 Answers1

0

You may use a UDP endpoint associated with a example destination IP address (or none, if you want the default path) as explained in this answer:

https://stackoverflow.com/a/27376368/2557128

NetMage
  • 26,163
  • 3
  • 34
  • 55