#include#include bool checkNetwork() { boost::asio::io_service ioService; boost::asio::ip::tcp::socket socket(ioService); boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string("8.8.8.8"), 53); socket.connect(endpoint); return true; } int main() { if (checkNetwork()) { std::cout << "Connected to network!" << std::endl; } else { std::cout << "Not connected to network!" << std::endl; } return 0; }
#includeIn the above example, we are using the Poco C++ Libraries to list all available network interfaces and check if any of them have an IPv4 address. If an interface with an IPv4 address is found, it is assumed that the system is connected to a network. Package/library used in the above examples: - Boost C++ Libraries - Poco C++ Libraries#include bool checkNetwork() { Poco::Net::NetworkInterface::List list = Poco::Net::NetworkInterface::list(); for (Poco::Net::NetworkInterface::List::iterator it = list.begin(); it != list.end(); ++it) { Poco::Net::IPAddress addr = it->address(); if (!addr.isLoopback() && addr.isIPv4()) { return true; } } return false; } int main() { if (checkNetwork()) { std::cout << "Connected to network!" << std::endl; } else { std::cout << "Not connected to network!" << std::endl; } return 0; }