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

}

相关推荐
Han.miracle16 分钟前
Redis 全套笔记:基础 API + 三大架构 + 缓存三大问题
java·windows·redis
pengyi8710152 小时前
私网IP映射公网实操步骤,搭配代理IP快速落地
网络·tcp/ip·智能路由器
水饺编程3 小时前
第5章,[标签 Win32] :获取设备环境的信息
c语言·c++·windows·visual studio
特长腿特长4 小时前
IP Tunneling 基础案例错误日志
网络·网络协议·tcp/ip
灯琰15 小时前
frp 部署
tcp/ip
CHENKONG_CK5 小时前
智流链驱动 RFID 混流装配,赋能汽车精益生产
网络·人工智能·tcp/ip·自动化·射频工程·rfid
yuanpan5 小时前
Python 调用 DLL 动态库入门:Windows 下调用 C++ 与 C# 动态库完整示例
c++·windows·python
IPDEEP全球代理5 小时前
美国纽约IP和普通美国IP有什么区别?
网络·网络协议·tcp/ip
张赐荣5 小时前
一键禁用 Ctrl+Alt+Shift+Windows 打开 Office 全局快捷键实用工具
windows
鸽芷咕6 小时前
【已解决】Claude Code 报错“claude.exe 与 Windows 不兼容“的解决方法
windows·claude