虎門仿做網(wǎng)站一鍵生成個(gè)人網(wǎng)站
為什么不推薦?boost::asio::ip::address::from_string、boost::asio::ip::address::make_string 函數(shù)來把字符串轉(zhuǎn)換為 ip 地址呢?
這是因?yàn)橛衅脚_(tái)、編譯器兼容性,在 android 平臺(tái)上面,使用這兩個(gè)函數(shù)會(huì)導(dǎo)致崩潰問題,在一些 clang 編譯器上面也會(huì)導(dǎo)致崩潰問題。
所以人們必須實(shí)現(xiàn)這兩個(gè)函數(shù)。
? ? boost::asio::ip::address ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?StringToAddress(const char* s, boost::system::error_code& ec) noexcept;
? ? inline boost::asio::ip::address ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? StringToAddress(const std::string& s, boost::system::error_code& ec) noexcept {?
? ? ? ? return StringToAddress(s.data(), ec);?
? ? }
? ? inline boost::asio::ip::address ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? StringToAddress(const ppp::string& s, boost::system::error_code& ec) noexcept {?
? ? ? ? return StringToAddress(s.data(), ec);?
? ? }
?
源實(shí)現(xiàn):
// On the Android platform, call: boost::asio::ip::address::from_string function will lead to collapse, // Only is to compile the Release code and opened the compiler code optimization.boost::asio::ip::address StringToAddress(const char* s, boost::system::error_code& ec) noexcept {ec = boost::asio::error::invalid_argument;if (NULL == s || *s == '\x0') {return boost::asio::ip::address_v4::any();}struct in_addr addr4;struct in6_addr addr6;if (inet_pton(AF_INET6, s, &addr6) > 0) {boost::asio::ip::address_v6::bytes_type bytes;memcpy(bytes.data(), addr6.s6_addr, bytes.size());ec.clear();return boost::asio::ip::address_v6(bytes);}else if (inet_pton(AF_INET, s, &addr4) > 0) {ec.clear();return boost::asio::ip::address_v4(htonl(addr4.s_addr));}else {return boost::asio::ip::address_v4::any(); }}
}