简体   繁体   中英

Determine whether someone is hard-wired to router in C++

We are trying to come up with the best way to detect if a user is hard-wired to their router in a C++ program being developed in Qt. It is okay if they are on WiFi also, but there needs to be a hard-wire present, as well.

This would only need to work on Windows but it would need to work on XP, Vista, 7, 8, and 8.1.

We tried the QNetworkConfiguration::BearerType which was promising but didn't seem to give any definitive results.

Has anyone ever done this or have an idea on how to? What is the best way? And if the answer involves QNetworkConfiguration, how do you figure it for the possibility that someone may be hard wired thru the Local Area Connection?

EDIT FOR CLARITY We employ work-from-home sales agents that use a softphone to make calls. We require them to be hard-wired to their router for better call quality. Since they are work from home, it is very hard to keep them plugged in without constant monitoring. They use a custom interface that we have control over and would like to be able to do this check before activating the interface, as well as periodically re-checking to ensure they stay plugged in.

No, this is not possible. A computer could be connected through a chain of a dozen switches (or worse, hubs) or a mile of untwisted pair cabling and there is no way you could specifically check that through software.

What you can do instead is measure the quality of the connection (ping time to default gateway and/or some known good external host, packet loss to same, etc).

As stated by Andrew (+1!) you should check the quality of the connection, more than the type/topology. That could be quite pointless since (i) topology can vary a lot and (ii) several other factors can affect the connection, apart from the used bearer.

That said, QNetworkConfiguration is still the answer to the bearer checking question but via the QNetworkConfigurationManager class. Indeed, a valid QNetworkConfiguration can be available to you even if the corresponding (wired or not wired) connection is not available, at the moment.

You should code something like this:

QNetworkConfiguration cfg;    
QNetworkConfigurationManager ncm;
auto nc = ncm.allConfigurations(QNetworkConfiguration::Active);   //ACTIVE ONES

for (auto &x : nc)
{
    if (x.bearerType() == QNetworkConfiguration::BearerEthernet)
    {
        cfg = x;
        // other stuff that pleases you
    }
}

Mind that the results are somewhat frozen to the time the QNetworkConfigurationManager was created. To update the list, before skimming it, you can call the (time consuming) updateConfigurations() .

Finally, by exploiting the configurationChanged(const QNetworkConfiguration & config) signal, you can track down status changes for your LAN connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM