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

}

相关推荐
足球中国3 小时前
nuget把缓存搬离C盘
c语言·windows·缓存
VX_184 小时前
域名SSL证书监测平台
网络·网络协议·ssl
love530love5 小时前
ComfyUI 插件发布 GitHub Release + Comfy Registry (官方节点商店)完整复盘教程(从零开始)
人工智能·windows·github·devops
ofoxcoding5 小时前
Windows 上 Codex 报 “failed to start codex app-server“:成因与修复(2026)
windows·ai
tiantianuser7 小时前
NVME-oF IP 设计7 :设计扫盲5
网络协议·rdma·roce v2·nvme of
ShineWinsu7 小时前
对于Linux:TCPsocket编程基础的解析
linux·网络协议·tcp/ip·面试·笔试·进程·远程指令
每天都进步一点点7 小时前
Windows 系统安装 Python 3.11+ 详细教程
windows·python3.11
meilindehuzi_a8 小时前
浏览器端 HTTP 流式通信详解:从 SSE 到 EventSource、fetch 与 ReadableStream
网络·网络协议·http
开发小程序的之朴8 小时前
安企CMS的安装-PHPStudy(小皮面板)部署
windows·mysql·nginx
love530love8 小时前
【硬核排障 & 猴子补丁 & 幽灵节点】SageAttention GQA 崩溃与 Flash Attention 兼容性修复全记录
ide·人工智能·windows·comfyui·sageattention·windows cuda