简体   繁体   中英

How to convert with boost:asio:ip:address IPV6 to IPV4

I write a litte program with boost::asio to convert ipv6 in ipv4, it compiles well but if I run my program I got:

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)

my code:

#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;
}

This is by design. A boost::asio::ip::address is either an IPv6 address, or an IPv4 address. It is not both at once.

It is simply not meaningful to talk about "convert IPv6 to IPv4". The closest thing would to be constructing a table of "known addresses for a particular host" and looking within that for an address of a different kind to the one you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM