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 小时前
宝塔SSL自动续签
网络·网络协议·ssl
上海云盾商务经理杨杨1 小时前
高防IP如何抵御CC攻击?2025年全面防护机制解析
网络·网络协议·tcp/ip·网络安全
李白你好1 小时前
Ping命令为何选择ICMP而非TCP/UDP?
网络协议·tcp/ip·udp
bug攻城狮1 小时前
CentOS 7 设置静态 IP 地址
linux·tcp/ip·centos
high20112 小时前
【 运维相关】-- HTTP 压测/负载发生器之新秀 oha
运维·网络协议·http
帅得不敢出门3 小时前
macOS苹果电脑运行向日葵远程控制软件闪退
windows·macos·远程控制·向日葵
AD钙奶-lalala5 小时前
HTTP response code 200 206 416详解
网络·网络协议·http
UNbuff_06 小时前
Linux ip 命令使用指南
linux·网络·tcp/ip
蒋星熠7 小时前
WebSocket网络编程深度实践:从协议原理到生产级应用
网络·数据库·redis·python·websocket·网络协议·微服务
dreams_dream8 小时前
标准 HTTP 状态码详解
网络·网络协议·http