#include "stdafx.h"
#include "windows.h"
#pragma comment(lib,"ws2_32.lib")
//拆分成两个功能是为了使用灵活,无任何技术含量,纯代码块备份一下方便使用
DWORD GetIpAddrByName(char* name) //通过域名获取IP的整形格式
{
struct hostent* host = gethostbyname (name);
if (!host) return NULL;
DWORD dwIP = *(DWORD*)host->h_addr_list[0];
return dwIP;
}
char* Int2IpStr(int nIP) //转换整形IP到字符串形式
{
struct in_addr addr;
addr.S_un.S_addr = nIP;
return inet_ntoa(addr );
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2,2), &wsadata);
DWORD dwIP = GetIpAddrByName("www.baidu.com");
printf("IP string of %d :%s\n", dwIP, Int2IpStr(dwIP));
return 0;
}