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

the_venus the_venus 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请问一下,我用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);
}
}
...全文
评论 4
收藏 已收藏
分享
返回顶部
the_venus
网络编程 18,356 成员 64,161 内容