diff options
| author | Mate Barany <mate.barany@qt.io> | 2025-08-14 16:47:22 +0200 |
|---|---|---|
| committer | Mate Barany <mate.barany@qt.io> | 2025-08-20 18:37:28 +0200 |
| commit | 380ed8d4876762214ab0ea834567b338e6cf7655 (patch) | |
| tree | 5af69b3e12c816f2d3c05faa1e0d45ad6b7c0cf8 /src/network/doc/snippets/code | |
| parent | 0543158533016eff20b0a930922f411698b57fec (diff) | |
Add simple example how to test for connectivity with QNetworkinfo
Extended with a description that explains the shortcomings of this
approach.
Task-number: QTBUG-136625
Pick-to: 6.10
Change-Id: I07473b1395fcc6dec0917da4bcc1c3791c1ed839
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/network/doc/snippets/code')
| -rw-r--r-- | src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_reachability.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_reachability.cpp b/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_reachability.cpp new file mode 100644 index 00000000000..2c736f40c9a --- /dev/null +++ b/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_reachability.cpp @@ -0,0 +1,66 @@ +// Copyright (C) 2025 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//![file] +#include <QCoreApplication> +#include <QNetworkInformation> +#include <QHostAddress> +#include <QDebug> + +//! [0] +// Simple helper to decide whether an IP address is "local" +bool isLocalAddress(const QHostAddress &address) +{ + return address.isInSubnet(QHostAddress("192.168.0.0"), 16) || + address.isInSubnet(QHostAddress("10.0.0.0"), 8) || + address.isInSubnet(QHostAddress("172.16.0.0"), 12) || + address.isLoopback(); +} + + +int main(int argc, char *argv[]) +{ +//! [0] + QCoreApplication app(argc, argv); + + // Load the default backend for QNetworkInformation + if (!QNetworkInformation::loadDefaultBackend()) { + qWarning() << "Failed to load QNetworkInformation backend. Exiting."; + return 1; + } + + QNetworkInformation *networkInfo = QNetworkInformation::instance(); +//! [1] + // Target IP address (default: Google DNS) + QString targetIpStr = argc > 1 ? argv[1] : "8.8.8.8"; + QHostAddress targetIp(targetIpStr); + + if (targetIp.isNull()) { + qWarning() << "Invalid IP address:" << targetIpStr; + return 1; + } + + // Decide what level of reachability is needed for the target + QNetworkInformation::Reachability requiredReachability = + isLocalAddress(targetIp) + ? QNetworkInformation::Reachability::Local + : QNetworkInformation::Reachability::Online; + + // Fetch the current system-reported reachability + QNetworkInformation::Reachability currentReachability = networkInfo->reachability(); + + qDebug() << "Target IP:" << targetIp.toString(); + qDebug() << "Target is considered" + << (isLocalAddress(targetIp) ? "local/site." : "external/online."); + qDebug() << "Required reachability level:" << requiredReachability; + qDebug() << "Current reachability:" << currentReachability; + + if (currentReachability < requiredReachability) { + qWarning() << "Current network state may not allow reaching the target address."; + } else { + qDebug() << "Target may be reachable based on current network state."; + } +//! [1] + return 0; +} +//![file] |
