使用 boost asio 枚举我的卡的 ipv4 和 ipv6 地址

发布于 2024-11-15 06:37:54 字数 993 浏览 1 评论 0原文

我正在尝试枚举我的电脑的所有网卡(我有 2 张卡)的 ipv4 和 ipv6 地址。

我正在使用以下代码来执行此操作。

using boost::asio::ip::tcp;
boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(boost::asio::ip::host_name(),"");
    tcp::resolver::iterator it=resolver.resolve(query);

    while(it!=tcp::resolver::iterator())
    {
        boost::asio::ip::address addr=(it++)->endpoint().address();
        if(addr.is_v6())
        {
            std::cout<<"ipv6 address: ";
        }
        else
            std::cout<<"ipv4 address: ";

        std::cout<<addr.to_string()<<std::endl;

    }

该代码显示正确的 ipv4 地址,但不显示 ipv6。 输出

ipv6 address: ::1
ipv4 address: 192.168.10.200
ipv4 address: 192.168.10.236

这是我对 ipv6 知之甚少的 。当我使用 ipconfig/all 列出有关网络接口的信息时,我看到实际的 ipv6 地址是

fe80::226:5aff:fe14:5687%5 
fe80::225:64ff:feb2:4f61%4

有人可以指导我如何列出 ipv6 地址吗? 谢谢。

I am trying to enumerate ipv4 and ipv6 addresses of all the network cards(I have 2 cards) my pc.

I am using the following code to do that.

using boost::asio::ip::tcp;
boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(boost::asio::ip::host_name(),"");
    tcp::resolver::iterator it=resolver.resolve(query);

    while(it!=tcp::resolver::iterator())
    {
        boost::asio::ip::address addr=(it++)->endpoint().address();
        if(addr.is_v6())
        {
            std::cout<<"ipv6 address: ";
        }
        else
            std::cout<<"ipv4 address: ";

        std::cout<<addr.to_string()<<std::endl;

    }

The code displays correct ipv4 addresses but not ipv6. Here is the output

ipv6 address: ::1
ipv4 address: 192.168.10.200
ipv4 address: 192.168.10.236

I have very minimum knowledge of ipv6. When I list the information about network interface using ipconfig/all I see that the actual ipv6 addresses are

fe80::226:5aff:fe14:5687%5 
fe80::225:64ff:feb2:4f61%4

Can someone please guide me how to list the ipv6 addresses.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2024-11-22 06:37:54

如果平台是 Windows 7 SP1,则将跳过链接本地接口,因为它们被 Windows 标记为“SkipAsSource”,这意味着 getaddrinfo 不会返回它们,因此也不会 促进。

您可以尝试使用以下命令检查该标志:

netsh int ipv6 show addresses level=verbose

Address fe80::e0:0:0:0%14 Parameters
---------------------------------------------------------
Interface Luid     : Teredo Tunneling Pseudo-Interface
Scope Id           : 0.14
Valid Lifetime     : infinite
Preferred Lifetime : infinite
DAD State          : Deprecated
Address Type       : Other
Skip as Source     : **true**

If the platform is Windows 7 SP1 the link-local interfaces are being skipped as they are tagged "SkipAsSource" by Windows which means that getaddrinfo will not return them and hence neither will Boost.

You can try to inspect the flag with the following command:

netsh int ipv6 show addresses level=verbose

Address fe80::e0:0:0:0%14 Parameters
---------------------------------------------------------
Interface Luid     : Teredo Tunneling Pseudo-Interface
Scope Id           : 0.14
Valid Lifetime     : infinite
Preferred Lifetime : infinite
DAD State          : Deprecated
Address Type       : Other
Skip as Source     : **true**
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文