#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32")
int main()
{
WSADATA wSdata;
if (WSAStartup(MAKEWORD(2,2), &wSdata)!=0)
{
printf("WSAStartup() error %d", GetLastError());
WSACleanup();
system("pause");
return -1;
}
char hname[100]={0};
char buff[20]={0};
struct in_addr addr;
gethostname(hname, sizeof(hname)); //获得主机名
printf("hostname: %s/n", hname);
//
printf("*************/n");
hostent *p=gethostbyname(hname);
printf("p->h_name:%s/n", p->h_name);
printf("p->h_length:%d/n", p->h_length);
printf("p->h_addrtype:%d/n", p->h_addrtype);
printf("p->h_aliases[0]:%s/n", p->h_aliases[0]);
int i=0;
while(p->h_addr_list[i])
{
addr.S_un.S_addr = *(unsigned long *)p->h_addr_list[i];
printf("p->h_addr_list[%d]:%s/n",i , inet_ntoa(addr));
i++;
}
i=0;
while(p->h_aliases[i])
{
printf("p->h_aliases[%d]:%s/n",i ,p->h_aliases[i]);
i++;
}
printf("*************/n");
//gethostbyaddr()
in_addr addrss;
addrss.S_un.S_addr = inet_addr("127.0.0.1");
p = gethostbyaddr((char *)&addrss, sizeof(in_addr), AF_INET);
printf("p->h_name:%s/n", p->h_name);
printf("p->h_length:%d/n", p->h_length);
printf("p->h_addrtype:%d/n", p->h_addrtype);
printf("p->h_aliases[0]:%s/n", p->h_aliases[0]);
i=0;
while(p->h_addr_list[i])
{
addr.S_un.S_addr = *(unsigned long *)p->h_addr_list[i];
printf("p->h_addr_list[%d]:%s/n",i , inet_ntoa(addr));
i++;
}
//
printf("*************/n");
p=gethostbyname("www.baidu.com");
printf("p->h_name:%s/n", p->h_name);
printf("p->h_length:%d/n", p->h_length);
printf("p->h_addrtype:%d/n", p->h_addrtype);
printf("p->h_aliases[0]:%s/n", p->h_aliases[0]);
i=0;
while(p->h_addr_list[i])
{
addr.S_un.S_addr = *(unsigned long *)p->h_addr_list[i];
printf("p->h_addr_list[%d]:%s/n",i ,inet_ntoa(addr));
i++;
}
i=0;
while(p->h_aliases[i])
{
printf("p->h_aliases[%d]:%s/n",i ,p->h_aliases[i]);
i++;
}
system("pause");
WSACleanup();
return 0;
}