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

}

相关推荐
绝知此事2 小时前
Netty实战:从零构建高性能TCP通信服务(含心跳检测)
java·网络·spring boot·网络协议·tcp/ip
一个人旅程~3 小时前
linux如何“抢”过windows的usb移动硬盘权限对0磁道损坏的移动硬盘进行尝试修复
linux·windows·经验分享·电脑
Keano Reurink4 小时前
长尾关键词自动化扩展:从1个种子词到1000个长尾词
运维·windows·自动化
新时代农民工~5 小时前
PostgreSQL 主从复制(流复制)实战配置指南:Windows 环境详细步骤
数据库·windows·postgresql
cui_ruicheng6 小时前
Linux网络编程(七):TCP Socket编程与EchoServer
linux·服务器·网络·tcp/ip
AI周红伟6 小时前
通用业务智能体OpenClaw+Skills+RAG+Agent构建案例实操
大数据·人工智能·windows·百度·copilot
a58808116 小时前
WarCraft III《魔兽争霸3冰封王座》原版安装包——游戏玩法、配置要求与详细安装教程
windows·游戏·游戏程序
Chirp8 小时前
Windows下借助wsl2读取ext4格式磁盘
linux·windows
2601_957882248 小时前
多平台数字内容集群的版权合规与风控体系:从查重检测到IP保护的技术实践
人工智能·网络协议·tcp/ip
TechWayfarer8 小时前
营销反作弊实战:用IP归属地查询平台识别虚假流量
网络·python·网络协议·tcp/ip·数据分析