使用 boost asio 枚举我的卡的 ipv4 和 ipv6 地址
我正在尝试枚举我的电脑的所有网卡(我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果平台是 Windows 7 SP1,则将跳过链接本地接口,因为它们被 Windows 标记为“SkipAsSource”,这意味着
getaddrinfo
不会返回它们,因此也不会 促进。您可以尝试使用以下命令检查该标志:
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: