How to get the hostname of an ip address by using asio library?

房东的猫 提交于 2019-12-04 06:10:26

问题


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



来源:https://stackoverflow.com/questions/49303769/how-to-get-the-hostname-of-an-ip-address-by-using-asio-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!