Getting valid IP from IPHostEntry

穿精又带淫゛_ 提交于 2019-12-10 18:48:36

问题


I tried to get the IPAddress 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 in my computer which is connected to the router. It is ipv4 but this line of code gives me 4 IPAddress 3 of them are ipv6 and one is ipv4 which is the valid one. I like to ask why is that so ?

Thanks


回答1:


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


来源:https://stackoverflow.com/questions/5056838/getting-valid-ip-from-iphostentry

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!