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

}

相关推荐
云栖梦泽在2 小时前
Claude Code / Codex 使用卡顿怎么办?AI 编程 Agent 连接失败与网络排查思路
网络·人工智能·网络协议·chatgpt·性能优化
子不语1805 小时前
从0开始学习S7-1200+ET200SP(3)——两台S7-1200通过TCP连接
网络协议·学习·tcp/ip
Omics Pro5 小时前
首个针对生物医药LLM智能体的全流程过程级评测框架
数据库·人工智能·windows·redis·量子计算
红糖奶茶5 小时前
显卡打不上驱动的原因和解决办法
windows·电脑
折哥的程序人生 · 物流技术专研5 小时前
Java面试通关⑦:JavaWeb网络核心全集
网络协议·http·javaweb·校招·前后端交互·java面试·社招
huainingning6 小时前
锐捷ACL单向TCP互通组网-通过Established状态回包实现
服务器·网络·tcp/ip
一杯奶茶¥6 小时前
系统重装后如何使用万能驱动软件给新系统安装驱动
windows·电脑·系统重装
huainingning6 小时前
锐捷ACL单向TCP互通组网-使用TCP三次握手SYN包置位为1实现
服务器·网络·tcp/ip
儒雅的烤地瓜7 小时前
计算机网络 | 路由器局域网(内网)IP和外网(公网)IP的区别与联系
网关·tcp/ip·智能路由器·ip·外网·内网·nat技术
小蜗牛的路8 小时前
使用OpenSSL生成本地证书https+nginx
网络协议·nginx·https