#include "stdafx.h"
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;
// link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
void main()
{
char hostname[] = "www.baidu.com";
WSADATA wsData;
int status = WSAStartup(MAKEWORD(2,2),&wsData);
if (NO_ERROR != status)
{
cout << "WSAStartup Error" << endl;
return;
}
DWORD dwRetval;
struct addrinfo *result;
struct addrinfo hints;
struct addrinfo *ptr = NULL;
ZeroMemory(&hints,sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
dwRetval = getaddrinfo(hostname,NULL,&hints,&result);
TCHAR cIpBuffer[64] = _T("");
DWORD len = sizeof(cIpBuffer);
for (ptr = result; ptr != NULL; ptr=ptr->ai_next)
{
if (ptr->ai_family == AF_INET6)
{
WSAAddressToString(ptr->ai_addr, ptr->ai_addrlen,NULL,cIpBuffer,&len);
}
else
{
WSAAddressToString(ptr->ai_addr, ptr->ai_addrlen,NULL,cIpBuffer,&len);
}
}
freeaddrinfo(result);
WSACleanup();
wprintf(L"%s\n",cIpBuffer);
}
IP地址转换成域名:gethostbyaddr
IP地址解析
最新推荐文章于 2022-08-19 15:47:24 发布