在C++中使用Boost库获取本机的IP地址,可以通过Boost.Asio库来实现。下面是一个简单的示例,展示如何使用Boost.Asio获取本机的IP地址:
```cpp
include
include
include
int main() {
try {
boost::asio::io_context io_context;
boost::asio::ip::udp::resolver resolver(io_context);
boost::asio::ip::udp::resolver::results_type endpoints = resolver.resolve(boost::asio::ip::udp::v4(), "google.com", "www");
for (auto& endpoint : endpoints) {
std::string address = boost::asio::ip::address_v4(endpoint).to_string();
std::cout << "Local IP address: " << address << std::endl;