设为首页 加入收藏

TOP

VC获取本机IP地址列表
2014-11-23 21:25:17 】 浏览:521
Tags:获取 地址

#include
#include
#pragma comment(lib,"ws2_32.lib")

int doit(int, char **)
{
char host_name[255];
//获取本地主机名称
if (gethostname(host_name, sizeof(host_name)) == SOCKET_ERROR) {
printf("Error %d when getting local host name. ", WSAGetLastError());
return 1;
}
printf("Host name is: %s ", host_name);

//从主机名数据库中得到对应的“主机”
struct hostent *phe = gethostbyname(host_name);
if (phe == 0) {
printf("Yow! Bad host lookup.");
return 1;
}

//循环得出本地机器所有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 " , i, inet_ntoa(addr));
}

return 0;
}

int main(int argc, char *argv[])
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
return 255;
}

int retval = doit(argc, argv);

WSACleanup();

return retval;
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC读写INI文件 下一篇如何实现类的成员函数创建线程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目