首页 > 解决方案 > 如何使用 boost:asio:ip:address IPV6 转换为 IPV4

问题描述

我用 boost::asio 编写了一个小程序来将 ipv6 转换为 ipv4,它编译得很好,但是如果我运行我的程序,我得到:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::bad_cast> >' what(): std::bad_cast Abgebrochen (Speicherabzug geschrieben)

我的代码:

#include <iostream>
#include <boost/asio/ip/address.hpp>

using namespace std;
using namespace boost::asio::ip;

int main(){
    address myIP = address::from_string("fe80::c87f:db87:787b:a7c4");
    address_v6 myIP6 = myIP.to_v6();
    address_v4 myIP4 = myIP.to_v4();
    cout << myIP4.to_string() <<endl;
    return EXIT_SUCCESS;
}

标签: c++boostasio

解决方案


这是设计使然。Aboost::asio::ip::address是IPv6 地址IPv4地址。它不是同时发生的。

谈论“将IPv6转换为IPv4”根本没有意义。最接近的事情是构建一个“特定主机的已知地址”表,并在其中查找与您拥有的地址不同的地址。


推荐阅读