#include "Winsock2.h "
#pragma comment(lib, "Ws2_32.lib")/// 注意字符串需以\0结尾 。 例如:lpzSeverName = " www.baidu.com/0";
bool WINAPI Domain2IP(char* lpzSeverName, TString & strIp)
{
if (!lpzSeverName)return false;
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
ZeroMemory(&wsaData, sizeof(wsaData));
WSAStartup(wVersionRequested, &wsaData);
hostent* pHostent = gethostbyname(lpzSeverName);
sockaddr_in sa;
ZeroMemory(&sa, sizeof(sa));
memcpy(&sa.sin_addr.s_addr,pHostent-> h_addr_list[0],pHostent-> h_length);
std::string strTemp = inet_ntoa(sa.sin_addr);//IP
WSACleanup();
TCHAR* lpzResult = NULL ;
if (!strTemp.c_str())return false;
int nLen = 0;
#ifdef UNICODE
nLen = ::MultiByteToWideChar(CP_ACP, 0, strTemp.c_str(), -1, NULL, 0); //include '\0'
lpzResult = new wchar_t [nLen];
::MultiByteToWideChar(CP_ACP, 0, strTemp.c_str(), -1, lpzResult, nLen);
#else
nLen = strlen(lpzByte);
lpzResult = new char[nLen + 1];
StringCbCopy(lpzResult, nLen + 1, strTemp.c_str());
#endif
strIp = lpzResult;
SAFE_DELETE(lpzResult);
return true;
}
VC 根据域名获取IP
原创
©著作权归作者所有:来自51CTO博客作者风雨无阻2024的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
获取IP地址
端点(API接口)访问获取IP地址!
IP Java SpringBoot\ -
java根据表名称获取所有字段
java根据表名称获取所有字段
字段 java sql -
iOS 根据域名获取ip地址和端口 如何根据域名获取ip
以下是VC Socket初始化时用到的两个函数 一、WSAStartup函数 int WSAStartup
iOS 根据域名获取ip地址和端口 应用程序 主机名 #include -
javaxms和xmx 超过容器限制
一、JVM常用基础配置 - Xmn : 新生代内存上限值 - Xms : 整个堆区初始内存分配的 大小 - Xmx : 整个堆区内存分配的最大上限,推荐 - Xms 和 - Xmx 设置等同大小,避免动态回收消耗 资源 - XX:MetaspaceSize = (JDK1.8 支持 )
javaxms和xmx 超过容器限制 JVM配置 JVM优化 JVM实践 老年代