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

}

相关推荐
陈小于6 小时前
windows(x86-x64)下编译JCEF
windows
网络研究院7 小时前
Firefox 146 为 Windows 用户引入了加密本地备份功能
前端·windows·firefox
FL16238631297 小时前
打开事件查看器提示MMC无法创建管理单元的解决思路
windows
꧁坚持很酷꧂8 小时前
Windows安装Qt Creator5.15.2(图文详解)
开发语言·windows·qt
Heart_to_Yang9 小时前
Telnet 调试屏幕输出信息卡死问题解决
网络·windows·经验分享
橘子真甜~10 小时前
C/C++ Linux网络编程13 - 传输层TCP协议详解(面向字节流和有连接)
linux·运维·服务器·c语言·网络·c++·tcp/ip
专业开发者10 小时前
近距离检测功能亮点
网络·tcp/ip·安全
杼蛘10 小时前
XXL-Job工具使用操作记录
linux·windows·python·jdk·kettle·xxl-job
qq_2515335911 小时前
查找 Python 中对象使用的内存量
开发语言·windows·python
JH灰色11 小时前
【大模型】-LangChain--Agent
windows·microsoft·langchain