为什么域名解析结果不相同?

the_venus 2011-01-15 02:19:11
请问一下,我用gethostbyname()函数解析的域名怎么与用nslookpu 命令解析的不一样,与WSAAsyncGetHostByName()解析的也不一样:举个例子说明一下:
nslookpu www.tsinghua.edu.cn
E:\>nslookup www.tsinghua.edu.cn
Server: dns.whut.edu.cn
Address: 202.114.88.10

Non-authoritative answer:
Name: www.d.tsinghua.edu.cn
Address: 166.111.4.100
Aliases: www.tsinghua.edu.cn

我用我写的一个小程序来解析,结果如下:
Host Name:www.d.tsinghua.edu.cn
Alias:
www.tsinghua.edu.cn
192.108.58.0

程序源码如下:
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include<iostream>
using namespace std;
char *hostname="www.tsinghua.edu.cn";

int main()
{
WSADATA data;
if(WSAStartup(0x0202,&data)!=0)
{
cout<<"Failed to start up:"<<WSAGetLastError()<<endl;
return -1;
}
PHOSTENT pHost=gethostbyname(hostname);
if(pHost==NULL)
{
cout<<"Failed to resolve host name:"<<WSAGetLastError()<<endl;
}
cout<<"Host Name:"<<pHost->h_name<<endl;
cout<<"Alias:"<<endl;
for(int i=0;pHost->h_aliases[i]!=NULL;i++)
{
cout<<pHost->h_aliases[i]<<endl;
}
for(int j=0;pHost->h_addr_list[j]!=NULL;j++)
{
in_addr addr;
addr.S_un.S_addr=(LONG)pHost->h_addr_list[j];
cout<<inet_ntoa(addr)<<endl;
}
WSACleanup();
return 0;
}
这个是用WSAAsyncGetHostByName()函数解析的结果:
HostName:www.d.tsinghua.edu.cn
Aliases:
www.tsinghua.edu.cn
Address type:IPV4.
244.250.18.0


代码如下:
在类的前面定义了一个消息
#define WM_GETHOSTBYNAME

class DemoDlg
{
...
public:
afx_msg void OnGetHostByName(WPARAM wParam,LPARAM lParam);
...

private:
HANDLE m_hGetHost;
char m_host[MAXGETHOSTSTRUCT];
};


在cpp文件中如下:

CDemoDlg::CDemoDlg()
{
...
m_hGetHostByName=NULL;
}

void CDemoDlg::OnGetHostByName(WPARAM wParam,LPARAM lParam)
{
HANDLE hGetHost=(HANDLE)wParam;
if (hGetHost==m_hGetHost)
{
if (WSAGETASYNCERROR(lParam)!=0)
{
MessageBox("Failed to Get host by name!",NULL);
return;
}
PHOSTENT pHost=(PHOSTENT)m_host;
CEdit *pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);
CString strHostAddr;
strHostAddr.Empty();
strHostAddr+="HostName:";
strHostAddr+=pHost->h_name;
strHostAddr+="\r\n";
int i=0;
strHostAddr+="Aliases:\r\n";
while (pHost->h_aliases[i]!=NULL)
{
strHostAddr+=pHost->h_aliases[i];
strHostAddr+="\r\n";
i++;
}
if (pHost->h_addrtype==AF_INET)
{
strHostAddr+="Address type:IPV4.\r\n";
}
else if(pHost->h_addrtype==AF_INET6)
{
strHostAddr+="Address type:IPV6\r\n";
}
else
{
strHostAddr+="UnKnown address type.\r\n";
}
if (pHost->h_length==4)
{
IN_ADDR inaddr;
for (int j=0;pHost->h_addr_list[j]!=NULL;j++)
{
inaddr.S_un.S_addr=(LONG)pHost->h_addr_list[j];
strHostAddr+=inet_ntoa(inaddr);
strHostAddr+="\r\n";
}
}
pEdit->SetWindowText(strHostAddr);
}
}

void CDemoDlg::OnButton1()
{
m_hGetHost=WSAAsyncGetHostByName(m_hWnd,WM_GETHOSTBYNAME,"www.tsinghua.edu.cn",m_host,MAXGETHOSTSTRUCT);
if(m_hGetHost==NULL)
{
CString strErr;
strErr.Format("Failed to Get host by name:%d\n",WSAGetLastError());
MessageBox(strErr,NULL,MB_OK|MB_ICONERROR);
}
}
...全文
173 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
哈利路亚1874 2011-01-15
  • 打赏
  • 举报
回复
一个域名多个服务主机
汪宁宇 2011-01-15
  • 打赏
  • 举报
回复
这个教育站估计是双线的机房吧...

可能由于不同DNS的原因,造成解析结果不一样,很正常的!

PS:nslookup小工具实际上也是调用的gethostbyname()

covsno 2011-01-15
  • 打赏
  • 举报
回复
nslookpu
你用错了
the_venus 2011-01-15
  • 打赏
  • 举报
回复
有点错误呀
#define WM_GETHOSTBYNAME WM_USER+1
纠正一下!

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧