DerMike
DerMike

Reputation: 16190

Usable IP address to present to user

I'm writing a server/client distributed tool. Users will have to enter a network address at some point. I want to help my dear users determine the current IP address by telling them.

[Update] Client and Server run in the same network. (Or users would have access to an administrator) [/Update]

When I call Dns.GetHostAddresses(Dns.GetHostName()) I get a hole bunch of entries which are all correct but not truly helpful. By which heuristic could I select the most appropriate entry to present to the user?

Upvotes: 0

Views: 123

Answers (1)

ABH
ABH

Reputation: 3439

If I understood your question right, you can compare IPAddress.AddressFamily with AddressFamily.InterNetwork to filter down

if (ip.AddressFamily == AddressFamily.InterNetwork)

Here is the complete code

IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
    if (ip.AddressFamily == AddressFamily.InterNetwork)
    {
        // Local ip address
    }
}

AddressFamily.InterNetwork is the value against IPv4 addresses. In case of IPv6 you can use AddressFamily.InterNetworkV6. For complete listing, please check this link.

Upvotes: 2

Related Questions