/************************************************************************/
/* 域名到IP的转换 */
/************************************************************************//
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
const char str[20]={0};
char *hostIP=0;
void main()
{
//初始化库,没他不行
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2),&wsa)!=0)
{
printf("Winsock Initialization failed.\n");
return;
}
//gethostbyname获得域名对应的IP
hostent *hp = gethostbyname(str);
if (hp)
{
//inet_ntoa把IP转换成文本型
hostIP = inet_ntoa(*(struct in_addr *)*hp->h_addr_list);
printf("host IP:%s\n",hostIP);
}
}