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

}

相关推荐
不会敲代码11 天前
TCP/IP 与前端性能:从数据包到首次渲染的底层逻辑
前端·tcp/ip
S1998_1997111609•X1 天前
论mysql国盾shell-sfa犯罪行为集团下的分项工程及反向注入原理尐深度纳米算法下的鐌檵鄐鉎行为
网络·数据库·网络协议·百度·开闭原则
开开心心就好1 天前
仅168KB的桌面图标自动隐藏工具
windows·计算机视觉·计算机外设·excel·启发式算法·宽度优先·csdn开发云
怪兽软家1 天前
DaVinci Resolve/达芬奇 20安装教程及下载
windows·经验分享·生活
chao1898441 天前
完整MES系统实现 (C# 客户端服务器)
服务器·windows·c#
Hello_Embed1 天前
Windows 安装 Claude Code 并接入 模型
windows·笔记·ai编程
Muyuan19981 天前
28.Paper RAG Agent 开发记录:修复 LLM Rerank 的解析、Fallback 与可验证性
linux·人工智能·windows·python·django·fastapi
以太浮标1 天前
华为eNSP模拟器综合实验之- MGRE多点GRE隧道详解
运维·网络·网络协议·网络安全·华为·信息与通信
遇见火星1 天前
Nginx 负载均衡配置模板:轮询、权重、IP哈希、最少连接
tcp/ip·nginx·负载均衡
AxureMost1 天前
4DDiG DLL Fixe 1.0.8.2 系统DLL修复工具
windows