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

}

相关推荐
Moyu_Friday1 小时前
网络安全基础-计算机网络应用与协议
网络协议·计算机网络
IM架构师2 小时前
# 一次老旧PHPCMS网站入侵排查:从SQL注入到PHP后门,以及IP白名单止血
sql·tcp/ip·php
百事牛科技2 小时前
如何创建7Z压缩包?Windows下7-Zip操作方法
windows·7-zip
池央3 小时前
Windows本地部署Qwen3.8实战:LM Studio、Cherry Studio、远程API与Token鉴权
windows
徐同保12 小时前
免费ssl证书全流程AI配置(含七牛云cdn)
网络·网络协议·ssl
泡海椒15 小时前
JQuick-SQL 快速开始:注册内存表并执行查询
数据库·windows·sql
幻智星科技15 小时前
一个开源的纯FPGA网口采集卡
网络协议·fpga开发·udp
钱栈up17 小时前
耳机音效不对,我顺手查了系统更新,结果挖出个崩溃了整天的服务
windows·shell
xiao77mwq17 小时前
Wireshark 在 Windows 上没法按进程抓包,我补了一条路
windows