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

}

相关推荐
Tandy12356_1 小时前
手写TCP/IP协议——IP层输出处理
c语言·网络·c++·tcp/ip·计算机网络
luoganttcc1 小时前
tcp 三次 握手
网络·网络协议·tcp/ip
飞Link9 小时前
Windows 环境下 LaTeX 的安装配置教程
windows·vscode
R.lin10 小时前
windows MySQL解压版安装教程
windows·mysql·adb
常家壮10 小时前
Windows隐藏账号创建完全指南:技术原理与安全实践
windows·安全·渗透测试·后门·windows隐藏账号
老蒋新思维12 小时前
创客匠人峰会洞察:私域 AI 化重塑知识变现 —— 创始人 IP 的私域增长新引擎
大数据·网络·人工智能·网络协议·tcp/ip·创始人ip·创客匠人
真上帝的左手13 小时前
18. 操作系统-Windows-命令提示符
windows
GHL28427109015 小时前
文件重命名(C++源码)
前端·c++·windows
北京盟通科技官方账号16 小时前
Ixxat Mobilizer系列:助力汽车组件的高效下线测试
网络协议·机器人·自动化·汽车·制造
fred_kang16 小时前
win11上使用SecoClient登录报错出现的SV无法连接至服务器信息
windows