Dns.GetHostAddresses() only returns IPv6 addresses . How to get Local IPv4?

不羁的心 提交于 2019-12-11 07:45:18

问题


According to Dotnet reference ,

Dns.GetHostAddresses("") 

should return IPv4 addresses . However I am getting only IPv6 on my windows 7 machine. I tried the program on a different windows 7 still the same result. It only returned IPv4 correctly on an XP machine.

If I try ,

Dns.GetHostEntry("");

It now correctly returns one IPv4 and one IPv6 . How do I get IPv4 from GetHostAddresses() because I don't want to use GetHostEntry(). It looks up the DNS.

Also, say for a computer with 1 Network card, the code returns an array of 2 identical IPv6 . Is this normal ?


回答1:


This works for me:

IPHostEntry ipHostEntry = Dns.GetHostEntry("example.com");
IPAddress ipAddress = ipHostEntry.AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork); // ipv4

It's normal to have two ipv6 addresses, one which contains your MAC address which guarantees its uniqueness, and another which doesn't to increase your privacy/security. But I don't know why yours would be identical.



来源:https://stackoverflow.com/questions/18369759/dns-gethostaddresses-only-returns-ipv6-addresses-how-to-get-local-ipv4

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