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

}

相关推荐
胡闹548 小时前
Linux查询防火墙放过的端口并额外增加需要通过的端口命令
linux·运维·windows
北极糊的狐8 小时前
若依系统报错net::ERR_CONNECTION_TIMED_OUT的原因
java·windows·sql·mybatis
老蒋新思维9 小时前
创客匠人:认知即资产 ——AI 时代创始人 IP 知识变现的底层逻辑
网络·人工智能·网络协议·tcp/ip·重构·创始人ip·创客匠人
白驹过隙^^10 小时前
OB-USP-AGENT安装使用方法
数据库·经验分享·网络协议·tcp/ip·github·ssl
sdszoe492210 小时前
IP地址规划与VLSM技术
网络·网络协议·tcp/ip·vlsm·ip地址规划
北京耐用通信11 小时前
耐达讯自动化网关:用Profinet唤醒沉睡的DeviceNet流量计,省下60%改造费!
人工智能·科技·物联网·网络协议·自动化·信息与通信
45288655上山打老虎11 小时前
List容器
数据结构·windows·list
Baikal..12 小时前
CVE-2024-38077漏洞 2012R2系统更新失败
windows
yunmoon0112 小时前
一款专业的 Windows 恶意程序分析与清理工具
windows
白仑色12 小时前
java中的anyMatch和allMatch方法
java·linux·windows·anymatch·allmatch