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

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_listi != 0; ++i) {

struct in_addr addr;

memcpy(&addr, phe->h_addr_listi, sizeof(struct in_addr));

vIp.push_back(inet_ntoa(addr));

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

}

WSACleanup();

}

相关推荐
酒神dnspup3 小时前
CDN 回源检测怎么做?用 DNSPup 验证节点命中、源站暴露与多地可用性
运维·服务器·网络·网络协议·tcp/ip
面对疾风叭!哈撒给3 小时前
Windows 11 系统更新操作设置停止更新的时间
windows
Dawn-bit4 小时前
Linux网络进阶:TCP三次握手和四次挥手与TCP连接状态
linux·服务器·网络·tcp/ip·云计算·智能路由器
酒神dnspup5 小时前
Tcping 端口连通性检测教程:用 DNSPup 判断防火墙、监听与线路故障
网络·网络协议·tcp/ip·ip·dnspup
晓晓_za8986685 小时前
Geo 优化源码蒸馏词机制:地域词库构建与匹配逻辑详解
运维·tcp/ip·spring·缓存·ci/cd
Zguigo7 小时前
lesson30-32计算机网络第四章:网络层核心三课(作用、IP协议、IPv4地址)
tcp/ip·计算机网络·php
小飞侠在吗9 小时前
Windows 下 Nginx 访问 127.0.0.1 报 50x 错误:根因是路径里 \t 被当成 Tab
运维·windows·nginx
在世修行10 小时前
激光打标机TCP Socket通信协议:KZ指令集实战
tcp/ip·socket·自定义协议
2401_8949155312 小时前
Geo 优化源码部署实战:环境配置、参数调优完整指南
运维·服务器·数据库·tcp/ip·安全