1、尝试了 gethostbyname(...) 和 Qt598中的qhostinfo::fromname(...),都可以解析出来IP,但是有一个问题:域名指向的IP改变了,这两种方式需要等较长时间 才会解析到更新后的ip地址。而CMD中 nslookup却一下子就解析到了更新后的IP...
1.1、查了一下资料,貌似原因如下(大概的说下原理,可能不严谨准确):
(1)域名服务器在 域名的TTL时间段内 它不会去实时的查找域名对应的IP,而是要等到TTL超时 域名服务器才回去再次查询 然后更新新的地址。
(2)又∵ gethostbyname 和 qhostinfo::fromname 都没有参数可以输入 DNS服务器地址,猜测可能使用的是 OS中设置的DNS服务器。
ZC:综上2个原因 造成了这个现象
ZC:改 OS的DNS设置的话,如果 这个DNS服务器不是 常用上网的DNS服务器的话,到时候还要改回来,比较麻烦,暂不考虑使用
1.2、于是猜测,指定 DNS解析服务器的话,就能解决这个现象
2、C++实现DNS域名解析 - Snser - 博客园.html(https://www.cnblogs.com/snser/p/4101729.html)
ZC:DNS解析的 数据包结构、原理、代码 都在这个文章中查看
ZC:只解析IPv4地址的精简版代码在 2.2
2.1、这是文章中的代码(少许改动,运行OK)(项目->属性->C/C++->预处理器->"预处理器定义"中添加 "_ITERATOR_DEBUG_LEVEL=0")
(1)CDNSLookup.h
#pragma once // C++实现DNS域名解析 - Snser - 博客园.html (https://www.cnblogs.com/snser/p/4101729.html) #define _CRT_SECURE_NO_WARNINGS //这里需要导入库 Ws2_32.lib,在不同的IDE下可能不太一样 //#pragma comment(lib, "Ws2_32.lib") #include <winsock2.h> #pragma comment(lib, "Ws2_32.lib") //#include <windows.h> #include <string> #include <vector> #define MAX_DOMAINNAME_LEN 255 #define DNS_PORT 53 #define DNS_TYPE_SIZE 2 #define DNS_CLASS_SIZE 2 #define DNS_TTL_SIZE 4 #define DNS_DATALEN_SIZE 2 #define DNS_TYPE_A 0x0001 //1 a host address #define DNS_TYPE_CNAME 0x0005 //5 the canonical name for an alias #define DNS_PACKET_MAX_SIZE (sizeof(DNSHeader) + MAX_DOMAINNAME_LEN + DNS_TYPE_SIZE + DNS_CLASS_SIZE) struct DNSHeader { USHORT usTransID; //标识符 USHORT usFlags; //各种标志位 USHORT usQuestionCount; //Question字段个数 USHORT usAnswerCount; //Answer字段个数 USHORT usAuthorityCount; //Authority字段个数 USHORT usAdditionalCount; //Additional字段个数 }; class CDNSLookup { public: CDNSLookup(); ~CDNSLookup(); BOOL DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<ULONG> *pveculIPList = NULL, std::vector<std::string> *pvecstrCNameList = NULL, ULONG ulTimeout = 1000, ULONG *pulTimeSpent = NULL); BOOL DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<std::string> *pvecstrIPList = NULL, std::vector<std::string> *pvecstrCNameList = NULL, ULONG ulTimeout = 1000, ULONG *pulTimeSpent = NULL); private: BOOL Init(); BOOL UnInit(); BOOL DNSLookupCore(ULONG ulDNSServerIP, char *szDomainName, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG ulTimeout, ULONG *pulTimeSpent); BOOL SendDNSRequest(sockaddr_in sockAddrDNSServer, char *szDomainName); BOOL RecvDNSResponse(sockaddr_in sockAddrDNSServer, ULONG ulTimeout, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG *pulTimeSpent); BOOL EncodeDotStr(char *szDotStr, char *szEncodedStr, USHORT nEncodedStrSize); BOOL DecodeDotStr(char *szEncodedStr, USHORT *pusEncodedStrLen, char *szDotStr, USHORT nDotStrSize, char *szPacketStartPos = NULL); private: BOOL m_bIsInitOK; SOCKET m_sock; };
(2)CDNSLookup.cpp
#include "CDNSLookup.h" #include <stdio.h> #include <string.h> CDNSLookup::CDNSLookup() : m_bIsInitOK(FALSE), m_sock(INVALID_SOCKET) { m_bIsInitOK = Init(); } CDNSLookup::~CDNSLookup() { UnInit(); } BOOL CDNSLookup::DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG ulTimeout, ULONG *pulTimeSpent) { return DNSLookupCore(ulDNSServerIP, szDomainName, pveculIPList, pvecstrCNameList, ulTimeout, pulTimeSpent); } BOOL CDNSLookup::DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<std::string> *pvecstrIPList, std::vector<std::string> *pvecstrCNameList, ULONG ulTimeout, ULONG *pulTimeSpent) { std::vector<ULONG> *pveculIPList = NULL; if (pvecstrIPList != NULL) { std::vector<ULONG> veculIPList; pveculIPList = &veculIPList; } BOOL bRet = DNSLookupCore(ulDNSServerIP, szDomainName, pveculIPList, pvecstrCNameList, ulTimeout, pulTimeSpent); if (bRet) { pvecstrIPList->clear(); char szIP[16] = { '