已经问过here和here以及here这样的问题但是没有一个答案似乎有效,所以我发布实际代码并显示它在ipv6上运行有能力的机器,希望有人能提出解决方案
以下是从这两个问题中获取的代码
using System;
public class HelloWorld
{
static public void Main ()
{
string hostName = "google.com"; // uri.DnsSafeHost;
Console.WriteLine("DNS.GetHostAddresses: " + hostName);
var hostAddresses = System.Net.Dns.GetHostAddresses(hostName);
Console.WriteLine("DNS.NumAddreses:" + hostAddresses.Length);
foreach (System.Net.IPAddress hostAddress in hostAddresses)
{
Console.WriteLine(
"addr: " + hostAddress.ToString() +
" family: " + hostAddress.AddressFamily.ToString());
}
System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(hostName);
Console.WriteLine("DNS.GetHostEntry NumAddresses:" + ipHostEntry.AddressList.Length);
foreach (System.Net.IPAddress hostAddress in ipHostEntry.AddressList)
{
Console.WriteLine(
"addr: " + hostAddress.ToString() +
" family: " + hostAddress.AddressFamily.ToString());
}
}
}
我使用ipv6在Ubuntu 14.04上运行它。这里有一些可能相关的信息
# ip -6 addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qlen 1000
inet6 2400:6180:0:d0::691:9001/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::601:eeff:fe7e:201/64 scope link
valid_lft forever preferred_lft forever
Google.com证明有一个ipv6地址
# dig google.com AAAA +short
2404:6800:4003:c00::8a
显示我可以直接在其ipv6地址
上pinggoogle.com
# ping6 2404:6800:4003:c00::8a
PING 2404:6800:4003:c00::8a(2404:6800:4003:c00::8a) 56 data bytes
64 bytes from 2404:6800:4003:c00::8a: icmp_seq=1 ttl=58 time=2.25 ms
64 bytes from 2404:6800:4003:c00::8a: icmp_seq=2 ttl=58 time=2.12 ms
^C
显示我的单声道版
# mono --version
Mono JIT compiler version 4.2.3 (Stable 4.2.3.4/832de4b Wed Mar 16 13:19:08 UTC 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: amd64
Disabled: none
Misc: softdebug
LLVM: supported, not enabled.
GC: sgen
# mcs --version
Mono C# compiler version 4.2.3.0
编译并运行上面的示例
# mcs dns.cs
# mono dns.exe
DNS.GetHostAddresses: google.com
DNS.NumAddreses:6
addr: 74.125.200.102 family: InterNetwork
addr: 74.125.200.139 family: InterNetwork
addr: 74.125.200.138 family: InterNetwork
addr: 74.125.200.101 family: InterNetwork
addr: 74.125.200.113 family: InterNetwork
addr: 74.125.200.100 family: InterNetwork
DNS.GetHostEntry NumAddresses:6
addr: 74.125.200.100 family: InterNetwork
addr: 74.125.200.113 family: InterNetwork
addr: 74.125.200.139 family: InterNetwork
addr: 74.125.200.102 family: InterNetwork
addr: 74.125.200.138 family: InterNetwork
addr: 74.125.200.101 family: InterNetwork
一些答案/评论建议.NET在仅ipv4机器上过滤掉ipv6。这显然不是仅限ipv4的机器。
注意:这里在同一台机器上的node.js中做同样的事情。它正确获取ipv6地址以及ipv4地址
# node
> require('dns').lookup("google.com", {all:true}, (err, addresses) => { console.log(addresses); });
GetAddrInfoReqWrap {
callback: { [Function: asyncCallback] immediately: true },
family: 0,
hostname: 'google.com',
oncomplete: [Function: onlookupall],
domain:
Domain {
domain: null,
_events: { error: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] } }
> [ { address: '74.125.200.101', family: 4 },
{ address: '74.125.200.138', family: 4 },
{ address: '74.125.200.102', family: 4 },
{ address: '74.125.200.139', family: 4 },
{ address: '74.125.200.113', family: 4 },
{ address: '74.125.200.100', family: 4 },
{ address: '2404:6800:4003:c00::65', family: 6 } ]
如何让DNS.GetHostAddresses
或DNS.GetHostEntry
同时返回ipv6和ipv4地址?
答案 0 :(得分:0)
这是从MSDN网站(https://msdn.microsoft.com/en-us/library/system.net.ipaddress(v=vs.110).aspx)获取的,它显示了如何查找地址上的所有相关信息,希望这可能有所帮助:
{{1}}
修改强>
对我的Win 10计算机的研究表明,IPv6地址仅针对本地主机返回,而不是针对具有GetHostEntry或GetHostAddresses的互联网主机。会更进一步。