I am trying to get hostname from UDP endpoint. However I do not know whether boost.asio supports IP->hostname conversion. Anyone can answer my question?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
getnameinfo is what you want.
getnameinfo((sockaddr*)&addr, sizeof(addr), hostname, sizeof(hostname), NULL, NULL, 0);
回答2:
#include<asio.hpp> #include<exception> #include<iostream> int main() { try { asio::io_service io_service; asio::ip::udp::resolver rsv(io_service); for(const auto &ele : rsv.resolve(asio::ip::udp::endpoint(asio::ip::address_v4({192,168,1,163}),0))) { std::cout<<ele.host_name()<<'\n'; } } catch(const std::exception &e) { std::cerr<<e.what()<<'\n'; return 1; } }
I have found out how to get host name by ip. Just directly resolve endpoint by using ip address