Windows c++获取本地ip

int GetLocalIp(vector<string> &vIp)

{

WSADATA wsaData;

int iRet = WSAStartup(((2 << 8) & 0xffff) | (2 & 0xff), &wsaData);

if (iRet != 0) {

std::cerr << "WSAStartup failed: " << iRet << std::endl;

return 1;

}

// 获取本机名

char hostname[256];

int result = gethostname(hostname, sizeof(hostname));

if (result != 0) {

std::cerr << "gethostname failed: " << WSAGetLastError() << std::endl;

WSACleanup();

return 1;

}

// 获取主机信息

struct hostent* phe = gethostbyname(hostname);

if (phe == nullptr) {

std::cerr << "gethostbyname failed: " << WSAGetLastError() << std::endl;

WSACleanup();

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));

vIp.push_back(inet_ntoa(addr));

std::cout << "Local IP " << i + 1 << ": " << inet_ntoa(addr) << std::endl;

}

WSACleanup();

}

相关推荐
m0_5493148624 分钟前
BIG-IP 介绍 2
tcp/ip·安全·负载均衡·big-ip·f5
卓码软件测评28 分钟前
【第三方软件测试测评机构:使用LoadRunner测试HTTPS/SSL协议应用的配置和证书处理 】
网络协议·测试工具·https·测试用例·ssl
比奇堡鱼贩34 分钟前
python第二次作业--函数
linux·运维·windows
七夜zippoe44 分钟前
gRPC高性能RPC框架实战:从Protocol Buffers到流式传输的完整指南
网络·python·网络协议·rpc·protocol
一路往蓝-Anbo1 小时前
第 1 篇:对象池模式 (Object Pool) —— 裸机下的动态内存革命
jvm·数据库·stm32·单片机·嵌入式硬件·网络协议·tcp/ip
冀辉2 小时前
局域网部署时实现基于https的业务系统访问
网络协议·http·https
七夜zippoe2 小时前
WebSocket实时通信系统构建:从握手协议到生产级实战
网络·python·websocket·网络协议·心跳
我送炭你添花2 小时前
树莓派 3B+ 部署 TR-069 ACS(自动配置服务器)GenieACS 实录
运维·服务器·网络协议
乐维_lwops3 小时前
利用Zabbix监控指定IP列表的ping
网络·tcp/ip·zabbix
小白郭莫搞科技3 小时前
鸿蒙跨端框架Flutter学习:ListView卡片样式详解
linux·服务器·windows