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

}

相关推荐
有代理ip35 分钟前
常见数据采集问题及实操解决方案
爬虫·网络协议·http·golang·ssl
匀泪39 分钟前
云原生(IP 透传,HAProxy 四层负载均衡实验)
tcp/ip·云原生·负载均衡
liwulin05061 小时前
【ESP32-S3】WINDOWS+VMware+ROS2+YDLIDA X2导航初步调试
windows·stm32·单片机
先做个垃圾出来………1 小时前
HTTP 请求中 Content-Type 常见取值
chrome·网络协议·http
之歆1 小时前
Linux 网络配置与 TCP/IP 协议
linux·网络·tcp/ip
xiaoliuliu123451 小时前
银河麒麟V10安装 openssl-1.1.1f-4.p12.ky10.x86_64.rpm 教程(含依赖解决)
windows
最贪吃的虎2 小时前
windows上如何可视化访问并远程操作linux系统上运行的浏览器或者linux可视化桌面
java·linux·运维·windows·分布式·后端·架构
rhett. li2 小时前
Windows系统中使用MinGW-W64(gcc/g++或LLVM)编译Skia源码的方法
c++·windows·ui·用户界面
bug智造2 小时前
Windows右下角时间不显示秒
windows
徐先生 @_@|||2 小时前
Windows 的高级进程监视器Microsoft - Process Monitor
windows