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

}

相关推荐
caimouse6 小时前
TDI (传输驱动接口 Transport Driver Interface) 详细分析
网络协议
NeilYuen9 小时前
用UDP实现向DNS服务器请求IP地址
服务器·tcp/ip·udp
shengnan_wsn10 小时前
【协议】【TCP】
网络·网络协议·tcp/ip
消失的旧时光-194311 小时前
第 3 篇:机器人 TCP 长连接如何稳定运行?心跳、掉线检测与自动重连
网络协议·tcp/ip·机器人
醉城夜风~11 小时前
(超详细)C++ STL list容器详解:从使用方法到双向链表底层原理全面解析
c++·windows
凤山老林12 小时前
在 Apple 芯片的 Mac 上通过 VMware Fusion 使用 Windows 11
windows·macos·vmware·虚拟机
不弃君12 小时前
在 Windows 上用 MinGW 搭建 lwIP 调试环境:方法与踩坑实录
windows·网络协议·tcp/ip·嵌入式·mingw·lwip
ITHAOGE1513 小时前
下载 | Win11 24H2 正式版更新!(系统ISO映像、多合一版本、26100.8973、Windows 11)
windows·科技·微软·电脑
NeilYuen14 小时前
实现TCP发送HTTP请求
网络协议·tcp/ip·http
天启HTTP15 小时前
爬虫频繁弹验证码?解析网站反爬检测机制
网络·爬虫·tcp/ip