c++ - 如何使用 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;
}
解决方案
这是设计使然。Aboost::asio::ip::address
是IPv6 地址或IPv4地址。它不是同时发生的。
谈论“将IPv6转换为IPv4”根本没有意义。最接近的事情是构建一个“特定主机的已知地址”表,并在其中查找与您拥有的地址不同的地址。
推荐阅读
- mysql - 加载数据文件不影响任何行
- tensorflow - TensorFlow-gpu 仅使用 CPU
- c++ - C++ PNG解码器错误
- react-native - React-native 占位符仅在 Android 上部分可见
- r - R函数中的可选参数
- java - 运行 JavaFX Gradle 应用程序时的 NoClassDefFoundError StringUtils 或 Gson
- c - 来自 epoll_wait 的 epoll 事件顺序
- javascript - 如何使用 javascript 显示或隐藏基于文本框输入的面板?
- html - 使用登录在网站后面卷曲 HTML 内容(手稿状态跟踪)
- javascript - 正则表达式:忽略转义项(Javascript)