Posted on 2011-07-26 14:05
魔のkyo 阅读(455)
评论(0) 编辑 收藏 引用
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char *argv[])
{
WSAData wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
char host_name[255];
//获取本地主机名称
if (gethostname(host_name, sizeof(host_name)) != SOCKET_ERROR)
{
printf("Host name is: %s\n", host_name);
//从主机名数据库中得到对应的“主机”
struct hostent *phe = gethostbyname(host_name);
if(phe != NULL)
{
//循环得出本地机器所有IP地址
for (int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
printf("Address %d : %s\n" , i, inet_ntoa(addr));
}
}
}
WSACleanup();
system("pause");
return 0;
}