Get Local IP-Address using Boost.Asio

后端 未结 5 740
悲&欢浪女
悲&欢浪女 2020-12-05 02:20

I\'m currently searching for a portable way of getting the local IP-addresses. Because I\'m using Boost anyway I thought it would be a good idea to use Boost.Asio for this t

5条回答
  •  攒了一身酷
    2020-12-05 03:02

    Assuming you have one network card / one local ip address:

    #include 
    namespace ip = boost::asio::ip;
    
    std::string getAddress()
    {
        boost::asio::io_service ioService;
        ip::tcp::resolver resolver(ioService);
    
        return resolver.resolve(ip::host_name(), "")->endpoint().address().to_string();
    }
    

提交回复
热议问题