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

}

相关推荐
小白白36125 分钟前
Pixelorama怎么用?像素画绘制和动画制作入门
windows
sukalot3 小时前
windows网络适配器驱动开发-WiFiCx Wi-Fi 7 功能要求(三)
windows·驱动开发
脚踏实地皮皮晨5 小时前
003003001_Grid控件
开发语言·windows·算法·c#·visual studio
ziguo11226 小时前
Windows API MessageBox 函数详解
c语言·c++·windows·visualstudio
Ivan CloudBay6 小时前
CDN 能完全隐藏源服务器 IP 吗?
运维·服务器·tcp/ip
sukalot7 小时前
windows网络适配器驱动开发-WiFiCx Wi-Fi 7 功能要求(二)
windows·驱动开发
funnycoffee1238 小时前
Cisco ip access-list序号重排方法
linux·网络·tcp/ip
乌萨奇也要立志学C++9 小时前
【Linux】传输层协议UDP与TCP详解
linux·tcp/ip·udp
山河不见老9 小时前
【Cursor 、Qoder安装问题】Cursor 、Qoder安装卡在“正在准备安装”问题排查及解决
人工智能·windows·编辑器
AI人工智能+电脑小能手9 小时前
【大白话说Java面试题 第210题】【10_网络协议篇】第1题:说说 TCP/IP 网络五层模型
java·网络协议·tcp/ip·计算机网络·网络五层模型