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

}

相关推荐
虾..1 小时前
Linux 基于TCP实现服务端客户端通信(线程池)
java·网络协议·tcp/ip
非凡ghost1 小时前
支持1000+格式:Wondershare Recoverit万能数据恢复
网络·windows·学习·软件需求·teamviewer
永远睡不够的入1 小时前
C++list详解
c++·windows·list
北京耐用通信1 小时前
耐达讯自动化CC linkie转Devicenet网关:架起三菱PLC与电导率仪跨协议“沟通之桥”
人工智能·物联网·网络协议·自动化·信息与通信
草根站起来1 小时前
OCSP中国泛域名SSL证书申请
网络·网络协议·ssl
longson.2 小时前
本地部署 MuseTalk 数字人(windows)
windows
机器小乙2 小时前
【开源】2 分钟在 Windows 上搭建 AI Agent 运行环境:MachineY Engine 使用指南
人工智能·windows·ai·开源·openclaw
橘子132 小时前
DNS域名系统
服务器·网络·网络协议
Xzq2105093 小时前
TCP协议
服务器·网络·tcp/ip
李子红了时3 小时前
Win10一键禁用自动更新和Windows Defender安全中心
windows·安全