如何使用Qt检查计算机的状态?
我正在尝试获取 LAN 中计算机的状态... 考虑过使用 QTcpSocket 但它并不是真正有效,因为端口也应该插入为:
socket->connectToHost("hostName", portNumber);
if (socket->waitForConnected(1000))
qDebug("Connected!");
任何人都可以向我演示一种更好的方法来检查计算机是否响应吗?
i'm trying to get computer's state in my LAN...
thought about using QTcpSocket but it's not realy effective since port also should be inserted as:
socket->connectToHost("hostName", portNumber);
if (socket->waitForConnected(1000))
qDebug("Connected!");
can anyone demonstare me a better way to check if computer is responding ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ping
参数可能会有所不同。例如,我相信在 Windows 上它会是
ping -n 1 "hostname"
。该示例应该适用于大多数非 Windows 版本。ping
Arguments may vary. For example, I believe it would be
ping -n 1 "hostname"
on Windows. The example should work on most non-Windows versions.您是否正在尝试检查您的本地计算机是否在网络上或者目标计算机是否在网络上?
没有一个好的跨平台方法可以做到这一点。
qt 上最接近的是 QNetworkInterface,并检查属性“ISup” - 它不是完美,如果您连接了网线但仅连接到路由器,则它可能处于活动状态;如果您有 3G 调制解调器但没有通话,则它可能处于非活动状态。
在 Windows 上检查 InternetGetConnectedState()
Are you trying to check if your local machine is on the network or if a target machine is ?
There isn't a good cross platform way of doing this.
The nearest on qt is QNetworkInterface, and check attribute "ISup" - it's not perfect, it may be active if you have a network cable connected but just to a router, and inactive if you have a 3G modem but aren't on a call.
On Windows check InternetGetConnectedState()
一种好的方法是验证他们是否可以使用 QHostInfo 解析域名。如果可以,那么他们可能可以访问互联网:
当然,您也可以尝试连接到主机,这更好地证明一切正常。我会异步而不是同步地进行,但这确实是最好的测试。
One good way is just to verify that they can resolve domain names using QHostInfo. If they can then they likely have internet access:
Of course, you could just try to connect to the host as well, which is even better proof that everything is working correctly. I would do it asynchronously rather than synchronously, but it's truly the best test.