tags:

views:

527

answers:

3

Hi, I have an application which runs a tool that requires network connection. Now my aim is to check whether the user has a network connection, if he don't have one, i can straight away display an error without proceeding further. If he has, he can continue working with my application. So my basic need is to check whether the user has a network connection or not. How i can achieve through Qt 4.4? I am using Windows XP.

+1  A: 

I assume that by "network connection" you mean "internet connection", i.e. you don't care about LAN or some ad-hoc networks between your desktop and mobile.

The easiest way is just to connect to the internet service your application needs and let OS handle the network request. If you get the reply, there is a connection, if the request times out, there is no connection.

You can check state of network interface through QNetworkInterface::flags(), but this doesn't give you the information about the network the interface is connected to: the interface might be up, but connected only to LAN without internet access.

chalup
+2  A: 

Hi, this code will help you.

#include <QtCore/QCoreApplication>
#include <QtNetwork/QNetworkInterface>

bool isConnectedToNetwork()
{
    QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
    bool result = false;

    for (int i = 0; i < ifaces.count(); i++)
    {
        QNetworkInterface iface = ifaces.at(i);
        if ( iface.flags().testFlag(QNetworkInterface::IsUp)
             && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
        {

#ifdef DEBUG
            // details of connection
            qDebug() << "name:" << iface.name() << endl
                    << "ip addresses:" << endl
                    << "mac:" << iface.hardwareAddress() << endl;
#endif

            // this loop is important
            for (int j=0; j<iface.addressEntries().count(); j++)
            {
#ifdef DEBUG
                qDebug() << iface.addressEntries().at(j).ip().toString()
                        << " / " << iface.addressEntries().at(j).netmask().toString() << endl;
#endif

                // we have an interface that is up, and has an ip address
                // therefore the link is present

                // we will only enable this check on first positive,
                // all later results are incorrect
                if (result == false)
                    result = true;
            }
        }

    }

    return result;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextStream output(stdout);


    output << endl << "Connection Status: " << ((isConnectedToNetwork())?"Connected":"Disconnected") << endl;


    return a.exec();
}
Anjum Kaiser
Like I wrote in my answer: QNetworkInterface will tell you only that connection to some network is up. It might be LAN with no internet access, ad-hoc wlan that connects your laptop with your mobile, virtual machine network, etc. I don't think that's the information the OP is looking for.
chalup
yep... and thats enough.. did the trick...
liaK
A: 

With Qt 4.7, you can use QNetworkConfiguration to check for checking connections http://doc.trolltech.com/main-snapshot/qnetworkconfiguration.html and even starting them e.g. on Symbian

guruz