Search code examples
c++qtqnetworkaccessmanager

Test a local connection with Qt


Is there a way to check if a cable is not unplugged by using Qt ?

I know the Ip adress that I want to contact, at first I was thinking of doing a ping request on this Ip Adress, but it seems to be too much complicated for this simple task.

So I'm thinking that maybe, there is a simple function able to do this in QNetwork library.

I search on the Internet but all the solutions are not testing one particular IP.

Thanks for your help.


Solution

  • Is there a way to check if a cable is not unplugged by using Qt ?

    You can achieve this for specified interface using QNetworkSession:

    QNetworkConfigurationManager nwManager (this);
    for (;;QThread::sleep (1) ) {
        bool isConfigurationFound {false};
        for (auto & configuration : nwManager.allConfigurations (/*QNetworkConfiguration::Active*/) ) {
            // Name depends on the environment: "Wired connection 1", "McDonaldsFreeWiFi", "Home", "eth0", "eth1", etc...
            if (isConfigurationFound = (configuration.name () == "eth0") ) {
                QNetworkSession session (configuration, this);
                session.open ();
                session.waitForOpened (/*timeout*/);
    
                qDebug () << "Session info:";
                qDebug () << "- usage: " << (session.isOpen () ? "Opened" : "Closed");
                qDebug () << "- state: " << (session.state  () == QNetworkSession::Connected ? "Connected" : "Not connected");
    
                break;
            }
        }
        qDebug () << (isConfigurationFound ? "" : "Configuration not found.");
    }
    

    If you launch this code with connected cable you get:

    "Session info:" 
    "- usage: Opened;" 
    "- state: Connected;" 
    

    If you unplugged cable you get:

    "Session info:" 
    "- usage: Closed" 
    "- state: Not connected"
    

    Here is full example (also available at GitLab):

    CMakeLists.txt

    cmake_minimum_required (VERSION 2.8.8)
    project (NaQt)
    find_package (Qt5Network)
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    set (NaQt_SOURCES ${PROJECT_SOURCE_DIR}/main.cpp)
    set (NaQt_HEADERS ${PROJECT_SOURCE_DIR}/NetworkAnalyzer.h)
    qt5_wrap_cpp (NaQt_HEADERS_MOC ${NaQt_HEADERS})
    
    add_executable (
        naqt
        ${NaQt_SOURCES}
        ${NaQt_HEADERS_MOC}
    )
    target_link_libraries (
        naqt
        Qt5::Core
        Qt5::Network
    )
    

    main.cpp

    #include <QtCore>
    #include "NetworkAnalyzer.h"
    
    int main (int argc, char * argv [])
    {
        QCoreApplication application (argc, argv);
        NetworkAnalyzer networkAnalyzer (& application);
        return application.exec ();
    }
    

    NetworkAnalyzer.h

    #ifndef QT_NETWORK_ANALYZER_H
    #define QT_NETWORK_ANALYZER_H
    
    #include <QThread>
    #include <QNetworkConfigurationManager>
    #include <QNetworkSession>
    #include <QDebug>
    
    class NetworkAnalyzer : public QObject
    {
    Q_OBJECT
    
    public:
        NetworkAnalyzer (QObject * parent = nullptr)
        {
            QNetworkConfigurationManager nwManager (this);
            for (;;QThread::sleep (1) ) {
                bool isConfigurationFound {false};
                for (auto & configuration : nwManager.allConfigurations (/*QNetworkConfiguration::Active*/) ) {
                    // Name depends on the environment: "Wired connection 1", "McDonaldsFreeWiFi", "Home", "eth0", "eth1", etc...
                    if (isConfigurationFound = (configuration.name () == "eth0") ) {
                        QNetworkSession session (configuration, this);
                        session.open ();
                        session.waitForOpened (/*timeout*/);
    
                        qDebug () << "Session info:";
                        qDebug () << "- usage: " << (session.isOpen () ? "Opened" : "Closed");
                        qDebug () << "- state: " << (session.state  () == QNetworkSession::Connected ? "Connected" : "Not connected");
    
                        break;
                    }
                }
                qDebug () << (isConfigurationFound ? "" : "Configuration not found.");
            }
        }
    };
    
    #endif//QT_NETWORK_ANALYZER_H