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

}

相关推荐
YCOSA20252 小时前
雨晨 Windows 11 IoT 企业版 LTSC 26H2 轻装 26300.9278 自用版
windows·物联网
丈剑走天涯4 小时前
基于 Python 3 语法的 PyCharm 练习示例,涵盖列表操作、类型判断及数据清洗等核心知识点
windows·python·pycharm
AxureMost4 小时前
Dopamine 3.0.10 音乐播放器
windows
MayZork4 小时前
Windows 与 Linux 下 vcpkg 的安装部署指南
linux·运维·windows
Source.Liu5 小时前
【Dioxus】Windows 环境下 Rust + Dioxus 安装配置笔记
windows·rust·dioxus
落魄大学生之流水线上谋生计6 小时前
LangGraph 基本用法指南
java·windows·python·langchain
sukalot7 小时前
windows 驱动实例分析系列: libusb驱动分析-应用层源码篇(三)
windows·驱动开发
Experience-摆渡10 小时前
自建 IP 轮换方案实战:健康探测 + 熔断剔除 + 粘性会话
网络·网络协议·tcp/ip
sxd200110 小时前
Vmware和multicast
linux·windows·vmware·无线桥接·multicast
for_ever_love__10 小时前
HTTP协议及网络请求与响应
网络·网络协议·http