使用 Qt 4.4 验证网络连接
我有一个运行需要网络连接的工具的应用程序。现在我的目标是检查用户是否有网络连接,如果没有网络连接,我可以立即显示错误而无需进一步操作。如果他有,他可以继续处理我的申请。所以我的基本需求是检查用户是否有网络连接。我如何通过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码会对你有所帮助。
this code will help you.
我认为“网络连接”是指“互联网连接”,即您不关心台式机和移动设备之间的 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.
使用 Qt 4.7,您可以使用 QNetworkConfiguration 来检查连接,甚至启动例如,在 Symbian 上。
With Qt 4.7, you can use QNetworkConfiguration for checking connections and even starting them e.g. on Symbian.