IPHostEntry hostinfo = Dns.GetHostEntry("www.google.com");
IPAddress[] ip = hostinfo.AddressList;
String[] names = hostinfo.Aliases;
使用上面的代码获得的names数组长度为0,而把Dns.GetHostEntry改为 Dns.Resolve后得到的结果却是大于0的,在MSDN中说Resolve已经过时应用GetHostEntry来代替,那么这两个功能应该是相同的吧,为什么会出现上面的情况呢?
IPAddress[] ip = hostinfo.AddressList;
String[] names = hostinfo.Aliases;
使用上面的代码获得的names数组长度为0,而把Dns.GetHostEntry改为 Dns.Resolve后得到的结果却是大于0的,在MSDN中说Resolve已经过时应用GetHostEntry来代替,那么这两个功能应该是相同的吧,为什么会出现上面的情况呢?
解决方案 »
- codesmith 如何生成解决方案?
- 保存上次选择路径
- 工作线程中,如何更新窗体界面上的listbox?
- 如何保存网页中的图像到SQLServer数据库(在线等待~~)
- 怎样实现同一台机子上拷贝文件的功能啊?
- 再求一次全局变量问题,,,谢谢
- 请问《C# primer》是否适合初学者?
- 难道 visual studio 2010 有超出重试计数限制 10
- “Excel.Application.Application()”表示“方法”,它在给定的上下文中无效,是什么原因?
- 不难,但我不会?我的visual c#.net中的一个功能不见了
- [100分] 不明白的“聚 合 关 系” 求一个最简单的示例代码 [100分]明白立即结分
- 对那个i++和++i推荐感到很囧!散分
{
IPAddress address;
IPHostEntry hostByName;
if (Logging.On)
{
Logging.Enter(Logging.Sockets, "DNS", "GetHostEntry", hostNameOrAddress);
}
s_DnsPermission.Demand();
if (hostNameOrAddress == null)
{
throw new ArgumentNullException("hostNameOrAddress");
}
if (TryParseAsIP(hostNameOrAddress, out address))
{
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
{
throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "hostNameOrAddress");
}
hostByName = InternalGetHostByAddress(address, true, false);
}
else
{
hostByName = InternalGetHostByName(hostNameOrAddress, true);
}
if (Logging.On)
{
Logging.Exit(Logging.Sockets, "DNS", "GetHostEntry", hostByName);
}
return hostByName;
}
[Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public static IPHostEntry Resolve(string hostName)
{
IPAddress address;
IPHostEntry hostByName;
if (Logging.On)
{
Logging.Enter(Logging.Sockets, "DNS", "Resolve", hostName);
}
s_DnsPermission.Demand();
if (hostName == null)
{
throw new ArgumentNullException("hostName");
}
if (TryParseAsIP(hostName, out address) && ((address.AddressFamily != AddressFamily.InterNetworkV6) || Socket.LegacySupportsIPv6))
{
hostByName = InternalGetHostByAddress(address, false, false);
}
else
{
hostByName = InternalGetHostByName(hostName, false);
}
if (Logging.On)
{
Logging.Exit(Logging.Sockets, "DNS", "Resolve", hostByName);
}
return hostByName;
}
关键是这个内部方法里面,没有渠道aliases:private static IPHostEntry NativeToHostEntry(IntPtr nativePointer)
{
hostent hostent = (hostent) Marshal.PtrToStructure(nativePointer, typeof(hostent));
IPHostEntry entry = new IPHostEntry();
if (hostent.h_name != IntPtr.Zero)
{
entry.HostName = Marshal.PtrToStringAnsi(hostent.h_name);
}
ArrayList list = new ArrayList();
IntPtr ptr = hostent.h_addr_list;
nativePointer = Marshal.ReadIntPtr(ptr);
while (nativePointer != IntPtr.Zero)
{
int newAddress = Marshal.ReadInt32(nativePointer);
list.Add(new IPAddress(newAddress));
ptr = IntPtrHelper.Add(ptr, IntPtr.Size);
nativePointer = Marshal.ReadIntPtr(ptr);
}
entry.AddressList = new IPAddress[list.Count];
list.CopyTo(entry.AddressList, 0);
list.Clear();
ptr = hostent.h_aliases;
nativePointer = Marshal.ReadIntPtr(ptr);
while (nativePointer != IntPtr.Zero)
{
string str = Marshal.PtrToStringAnsi(nativePointer);
list.Add(str);
ptr = IntPtrHelper.Add(ptr, IntPtr.Size);
nativePointer = Marshal.ReadIntPtr(ptr);
}
entry.Aliases = new string[list.Count];
list.CopyTo(entry.Aliases, 0);
return entry;
}