GetHostAddress() 不返回 IPv6,仅返回 IPv4

发布于 2024-12-04 21:00:56 字数 171 浏览 1 评论 0原文

当使用 Dns.GetHostAddresses("fred") 时,它仅返回 IPv4 地址作为响应。如何获取 IPv6 地址?我应该使用不同的方法吗?

Socket.OSSupportsIPv6 返回 true 并且 IPv6 连接工作正常。操作系统为Windows 2008 R2,.Net版本为3.5。

When using Dns.GetHostAddresses("fred") it returns only IPv4 addresses in response. How to get the IPv6 addresses as well? Is there different method I shall use?

Socket.OSSupportsIPv6 returns true and IPv6 connections work fine. The OS is Windows 2008 R2, .Net version is 3.5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-12-11 21:00:56

IPv6 仍然使用 DNS 来解析地址,因此为了使其正常工作,您需要为此域名在区域文件中添加一个 IPv6 条目。然后它就会正确解决。

我怀疑如果 IPv6 不可用,Ping 将回退到 IPv4,这并不完全是您指定“-6”标志后所希望的。

IPv6 still uses DNS to resolve addresses so in order for this to work you will need to add an IPv6 entry to your zone file for this domain name. It'll then resolve properly.

I suspect that Ping will fall back to IPv4 if an IPv6 is not available, not exactly what you'd hope once you've specified the '-6' flag.

爱给你人给你 2024-12-11 21:00:56

如果有人在 2021 年仍然遇到此问题,System.Net.Dns 类具有未记录的行为:当您唯一的 IPv6 地址除外时,它会过滤它认为无法访问的地址环回是链接本地。即使域名专门解析为 v6 地址(您可以尝试使用 v6.localhost.ayra.ch),也会执行此过滤,在这种情况下会引发异常。

如果您位于纯 IPv4 网络中,但希望能够解析 .NET 中的 v6 地址,则可以通过手动为设备分配自定义 IP 来解决此问题。为您的设备分配 IPv6 地址 2001:db8::1,前缀长度为 32,并且没有默认网关。到目前为止,我还没有发现任何东西因此而停止工作,.NET 现在将解析 IPv6。

请注意,建议的 IPv6 地址和范围保留用于文档中,这意味着您根本不应该在 Internet 上遇到此范围。如果您想确保您的选择不会与遵循此说明的其他人的地址冲突,我建议您在最后 3 个地址段中使用您的 mac 地址。

我制作了一个页面,可以使用您的 MAC 或随机生成这些地址: https://cable.ayra.ch/文档/

In case anyone is still having this problem in 2021, the System.Net.Dns class has an undocumented behavior: it filters addresses it thinks are unreachable when your only IPv6 address besides loopback is link-local. This filtering is performed even if the domain name exclusively resolves to a v6 address (you can try with v6.localhost.ayra.ch), in which case an exception is thrown.

If you're located in an IPv4 only network but want the ability to resolve v6 addresses in .NET, you can cheat your way around this problem by manually assigning your device a custom IP. Assign your device the IPv6 address 2001:db8::1 with a prefix length of 32 and no default gateway. I haven't found anything that stopped working so far because of this, and .NET will now resolve IPv6.

Note that the suggested IPv6 address and range is reserved for use in documentations, which means you should never encounter this range on the internet at all. If you want to make sure your choice doesn't conflicts with the address of someone else that follows this instruction, I suggest you use your mac address for the last 3 address segments.

I made a page that generates these address either with your MAC or randomly: https://cable.ayra.ch/docmac/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文