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

}

相关推荐
Li Ming&6 小时前
Windows Server 2019 操作系统《保姆级安装教程》
windows·信息与通信
隐擎fox7 小时前
高性能网络爬虫架构设计:基于 Python 的长连接复用与分布式会话池调度实践
分布式·python·网络协议·tcp/ip·高并发·网络爬虫、
陈天伟教授8 小时前
Windows操作技巧 -03
windows
IT摆渡者8 小时前
1panel 配置申请手动解析网站SSL证书
服务器·网络协议·ssl
易岳群9 小时前
物联网 MQTT 方案:服务端如何通过 MQTT 生命周期维护客户端设备状态
物联网·网络协议
TechExplorer3659 小时前
Windows 隐藏导航窗格【主文件夹】、【图库】、【OneDrive】
windows·onedrive
我一定会有钱10 小时前
官网谷歌浏览器离线安装包一键下载
windows
易点云徐源13 小时前
(已解决)Foxmail打开后2分钟左右崩溃
windows·foxmail
其实防守也摸鱼13 小时前
每天一个知识点——RCE漏洞
运维·服务器·数据库·windows·安全·github·漏洞
2501_9378609413 小时前
网络核心|OSI七层、TCP/IP五层模型,封装和分用详解
网络·网络协议·tcp/ip