使用 Qt 4.4 验证网络连接

发布于 2024-08-25 23:02:51 字数 142 浏览 4 评论 0原文

我有一个运行需要网络连接的工具的应用程序。现在我的目标是检查用户是否有网络连接,如果没有网络连接,我可以立即显示错误而无需进一步操作。如果他有,他可以继续处理我的申请。所以我的基本需求是检查用户是否有网络连接。我如何通过Qt 4.4实现?我使用的是Windows XP。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛夏尉蓝 2024-09-01 23:02:51

这段代码会对你有所帮助。

#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();
}

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();
}
余厌 2024-09-01 23:02:51

我认为“网络连接”是指“互联网连接”,即您不关心台式机和移动设备之间的 LAN 或某些临时网络。

最简单的方法就是连接到您的应用程序所需的互联网服务,并让操作系统处理网络请求。如果收到回复,则有连接,如果请求超时,则没有连接。

您可以通过 QNetworkInterface::flags() 检查网络接口的状态,但这不会为您提供有关接口所连接的网络的信息:接口可能已启动,但仅连接到 LAN,无法访问互联网。

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.

梦在深巷 2024-09-01 23:02:51

使用 Qt 4.7,您可以使用 QNetworkConfiguration 来检查连接,甚至启动例如,在 Symbian 上。

With Qt 4.7, you can use QNetworkConfiguration for checking connections and even starting them e.g. on Symbian.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文