Dns.GetHostAddresses()仅返回IPv6地址.如何获取本地IPv4? [英] Dns.GetHostAddresses() only returns IPv6 addresses . How to get Local IPv4?
问题描述
根据Dotnet参考
Dns.GetHostAddresses("")
应返回IPv4地址.但是,我在Windows 7计算机上仅获得IPv6.我在不同的Windows 7上仍然尝试了该程序,但结果仍然相同.它只能在XP计算机上正确返回IPv4.
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.
如果我尝试,
Dns.GetHostEntry("");
现在可以正确返回一个IPv4和一个IPv6. 如何从GetHostAddresses()获取IPv4,因为我不想使用GetHostEntry().它会查找DNS.
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.
也可以说,对于具有1个网卡的计算机,该代码返回2个相同IPv6的数组.这正常吗?
Also, say for a computer with 1 Network card, the code returns an array of 2 identical IPv6 . Is this normal ?
推荐答案
这对我有用:
IPHostEntry ipHostEntry = Dns.GetHostEntry("example.com");
IPAddress ipAddress = ipHostEntry.AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork); // ipv4
通常有两个ipv6地址,一个包含您的MAC地址以保证其唯一性,另一个不增加您的隐私/安全性.但是我不知道为什么你的人会完全相同.
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.
这篇关于Dns.GetHostAddresses()仅返回IPv6地址.如何获取本地IPv4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!