ubuntu(Linux) c++ 获取本机IPv4和ipv6、查询本机IPv4,IPv6
1.关于
演示环境:
Linux xxxxxxx 5.4.0-47-generic #51-Ubuntu SMP Fri Sep 4 19:50:52 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
代码来自 StackOverflow But,自己做了部分修改。
2.头文件
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
3.ipv4获取
调用函数get_ip_linux可以获取本机ipv4
int net_adapter_helper::get_ipv4_linux(std::list<std::string>& out_list_ip4)
{
return get_ip_linux(AF_INET, out_list_ip4);
}
4.ipv6获取
调用函数get_ip_linux可以获取本机ipv6
int net_adapter_helper::get_ipv6_linux(std::list<std::string>& out_list_ip6)
{
return get_ip_linux(AF_INET6, out_list_ip6);
}
5.get_ip_linux封装
这里封装,目的是为了更方便维护获取本机IP的代码。通过第一个参数控制当前需要获取ipv4还是ipv6。
int net_adapter_helper::get_ip_linux(int ipv4_6, std::list<std::string>& out_list_ip)
{
int ret_val = 0;
struct ifaddrs * ifAddrStruct = NULL;
void * tmpAddrPtr = NULL;
// 1.
ret_val = getifaddrs(&ifAddrStruct);
if (0 != ret_val)
{
ret_val = errno;
return ret_val;
}
// 2.
std::string str_ipvX;
int padress_buf_len = 0;
char addressBuffer[INET6_ADDRSTRLEN] = {0};
if (AF_INET6 == ipv4_6)
padress_buf_len = INET6_ADDRSTRLEN;
else
padress_buf_len = INET_ADDRSTRLEN;
while (NULL != ifAddrStruct )
{
if (ipv4_6 == ifAddrStruct->ifa_addr->sa_family )
{
// is a valid IP4 Address
tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
inet_ntop(ipv4_6, tmpAddrPtr, addressBuffer, padress_buf_len);
str_ipvX = std::string(addressBuffer);
out_list_ip.push_back(str_ipvX);
memset(addressBuffer, 0, padress_buf_len);
}
ifAddrStruct=ifAddrStruct->ifa_next;
}
return ret_val;
}
6.测试结果
- 6.1 调用代码
std::list<std::string> ipv4;
std::list<std::string> ipv6;
int ret_val = net_adapter_helper::get().get_ipv4_linux(ipv4);
if (0 != ret_val)
{
cout << "error, ipv4, id = " << ret_val << endl;
}
else
{
int index = 0;
for (auto item: ipv4)
{
cout << "index = " << ++index;
cout << ", ipv4 = " << item.c_str() << endl;
}
}
ret_val = net_adapter_helper::get().get_ipv6_linux(ipv6);
if (0 != ret_val)
{
cout << "error, ipv6, id = " << ret_val << endl;
}
else
{
int index = 0;
for (auto item: ipv6)
{
cout << "index = " << ++index;
cout << ", ipv6 = " << item.c_str() << endl;
}
}
- 6.2 输出结果:
ubuntu(Linux) c++ 获取本机IPv4和ipv6、查询本机IPv4,IPv6的更多相关文章
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- Linux中获取本机的最新IPv6地址_更新ddns的脚本
Linux中获取本机的最新IPv6地址_更新ddns的脚本 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-11-07. 运营商提供ipv6地址. 路由器后有台linux机器,通过e ...
- Linux下获取本机IP地址的代码
Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...
- linux下获取软件源码包 centos/redhat, debian/ubuntu
linux下获取软件源码包 centos/redhat, debian/ubuntu centos下: 1. yum install yum-utils 主要为了获取yumdownloader 2. ...
- windows10 c++获取本机IPv4,ipv6,查询本机Ipv4,ipv6
1.关于 演示环境: win10+VS2017 2.支持 需要下面的库支持: ws2_32.lib 需要下面的宏,添加到项目属性-> c/c++ -> 预处理器定义 _WINSOCK_DE ...
- Ubuntu Linux下设置IP的配置命令
Ubuntu Linux下设置IP的配置命令 今天装了Ubuntu,但是发现不能上网,开始排查问题: 1.首先确定网络连接是否正确,所用的网线是否可以正常工作 2.查看网卡是否能正常工作,检测的方法如 ...
- HOWTO install Oracle 11g on Ubuntu Linux 12.04 (Precise Pangolin) 64bits
安装了Ubuntu 12.04 64bit, 想在上面安装Oracle 11gr2,网上找了好多文档都没成功,最后完全参考了MordicusEtCubitus的文章. 成功安装的关键点:install ...
- [r]Ubuntu Linux系统下apt-get命令详解
Ubuntu Linux系统下apt-get命令详解(via|via) 常用的APT命令参数: apt-cache search package 搜索包 apt-cache show package ...
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
// all.h// 2005/06/20,a.m. wenxy #ifndef _ALL_H#define _ALL_H #include <memory.h>#include < ...
随机推荐
- Matlab矢量图图例函数quiverkey
Matlab自带函数中不包含构造 quiver 函数注释过程,本文参照 matplotlib 中 quiverkey 函数,构造类似函数为 Matlab 中 quiver 矢量场进行标注. quive ...
- MEGAN4,MEGAN5和MEGAN6的Linux安装和使用
目录 MEGAN 4 MEGAN 5 MEGAN 6 MEGAN(Metagenome Analyzer)是宏基因组学进行物种和功能研究的常用软件,实际上现在的Diamond+MEGAN6已经是一套比 ...
- leetcode刷题之数组NO.4
1.题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数 ...
- Redis学习小结
在7月中旬,我成功入职实习,通过进入公司,认识到了个人与企业巨大的差距,首先就是对于中间件的使用,ElasticSearch.Redis.Kafka等等,都是听过却从未使用过的,然而在任务下达之后,激 ...
- Linux网络(网络模型和收发流程)
网络模型 为了解决网络互联中异构设备的兼容性问题,并解耦复杂的网络包处理流程,国际标准化组织制定的开放式系统互联通信参考模型(Open System Interconnection Reference ...
- 基于 vue-cli 的 lib-flexible 适配
基于 vue-cli3.0 的 lib-flexible 适配方案 第一步:下载安装相关依赖 第二步:创建 vue.config.js 文件并配置 第三步:在 main.js 中引入 lib-flex ...
- Oracle删除重复数据记录
删除重复记录,利用ROWID 和MIN(或MAX)函数, ROWID在整个数据库中是唯一的,由Oracle自己产生和维护,并唯一标识一行(无论该表中是否有主键和唯一性约束),ROWID确定了每条记录在 ...
- 【spring AOP】AspectJProxyFactory
AspectJProxyFactory,可能大家对这个比较陌生,但是@Aspect这个注解大家应该很熟悉吧,通过这个注解在spring环境中实现aop特别的方便. 而AspectJProxyFacto ...
- 解决“该Jenkins实例似乎已离线”
在jenkins/pluginManager/advanced最下面 把:https://updates.jenkins-ci.org/update-center.json 换成: 1.http:// ...
- ASP.NET管道模型简析
我相信在第一次听到这个名词时,有的小伙伴会一脸懵,而且还有很多疑问,其实我在第一次接触这个概念时跟很多小伙伴一样一脸懵. 接下来我将以我自己的理解来讲述什么是管道模型. 什么是管道模型 首先有没有小伙 ...