Obtaining a valid IP address from IPHostEntry

I tried to get the IP address of my computer using this

        var ipadd = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ipAddress in ipadd.AddressList)
            Console.WriteLine("IP Address: {0}", ipAddress);

I have only one network card on my computer that is connected to the router. This is ipv4, but this line of code gives me 4 IPAddress 3 of them are ipv6, and one is ipv4, which is valid. I like to ask why this is so.

thank

+3
source share
1 answer
foreach (var addr in Dns.GetHostEntry(string.Empty).AddressList)
{
    if (addr.AddressFamily == AddressFamily.InterNetwork)
        Console.WriteLine("IPv4 Address: {0}", addr)
}
+3
source

Source: https://habr.com/ru/post/1793375/


All Articles