有没有办法使用Qt检查电缆是否未拔下?
我知道我要联系的Ip adress
,起初我想在ping request
上做Ip Adress
,但对于这个简单的任务来说似乎太复杂了。
所以我想也许,有一个简单的功能可以在QNetwork library
中执行此操作。
我在互联网上搜索,但所有解决方案都没有测试一个特定的IP。
感谢您的帮助。
答案 0 :(得分:6)
有没有办法检查是否使用Qt拔掉了电缆?
您可以使用QNetworkSession
:
QNetworkConfigurationManager nwManager (this);
for (;;QThread::sleep (1) ) {
bool isConfigurationFound {false};
for (auto & configuration : nwManager.allConfigurations (/*QNetworkConfiguration::Active*/) ) {
// Name depends on the environment: "Wired connection 1", "McDonaldsFreeWiFi", "Home", "eth0", "eth1", etc...
if (isConfigurationFound = (configuration.name () == "eth0") ) {
QNetworkSession session (configuration, this);
session.open ();
session.waitForOpened (/*timeout*/);
qDebug () << "Session info:";
qDebug () << "- usage: " << (session.isOpen () ? "Opened" : "Closed");
qDebug () << "- state: " << (session.state () == QNetworkSession::Connected ? "Connected" : "Not connected");
break;
}
}
qDebug () << (isConfigurationFound ? "" : "Configuration not found.");
}
如果您使用连接电缆启动此代码,则会获得:
"Session info:"
"- usage: Opened;"
"- state: Connected;"
如果拔下电缆,你会得到:
"Session info:"
"- usage: Closed"
"- state: Not connected"
以下是完整示例(也可在GitLab处获得):
CMakeLists.txt
cmake_minimum_required (VERSION 2.8.8)
project (NaQt)
find_package (Qt5Network)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set (NaQt_SOURCES ${PROJECT_SOURCE_DIR}/main.cpp)
set (NaQt_HEADERS ${PROJECT_SOURCE_DIR}/NetworkAnalyzer.h)
qt5_wrap_cpp (NaQt_HEADERS_MOC ${NaQt_HEADERS})
add_executable (
naqt
${NaQt_SOURCES}
${NaQt_HEADERS_MOC}
)
target_link_libraries (
naqt
Qt5::Core
Qt5::Network
)
main.cpp
#include <QtCore>
#include "NetworkAnalyzer.h"
int main (int argc, char * argv [])
{
QCoreApplication application (argc, argv);
NetworkAnalyzer networkAnalyzer (& application);
return application.exec ();
}
NetworkAnalyzer.h
#ifndef QT_NETWORK_ANALYZER_H
#define QT_NETWORK_ANALYZER_H
#include <QThread>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QDebug>
class NetworkAnalyzer : public QObject
{
Q_OBJECT
public:
NetworkAnalyzer (QObject * parent = nullptr)
{
QNetworkConfigurationManager nwManager (this);
for (;;QThread::sleep (1) ) {
bool isConfigurationFound {false};
for (auto & configuration : nwManager.allConfigurations (/*QNetworkConfiguration::Active*/) ) {
// Name depends on the environment: "Wired connection 1", "McDonaldsFreeWiFi", "Home", "eth0", "eth1", etc...
if (isConfigurationFound = (configuration.name () == "eth0") ) {
QNetworkSession session (configuration, this);
session.open ();
session.waitForOpened (/*timeout*/);
qDebug () << "Session info:";
qDebug () << "- usage: " << (session.isOpen () ? "Opened" : "Closed");
qDebug () << "- state: " << (session.state () == QNetworkSession::Connected ? "Connected" : "Not connected");
break;
}
}
qDebug () << (isConfigurationFound ? "" : "Configuration not found.");
}
}
};
#endif//QT_NETWORK_ANALYZER_H
答案 1 :(得分:1)
尝试在预定义端口上打开指定IP地址的套接字:
QTcpSocket tester;
tester.connectToHost(address, port);
if(tester.waitForConnected(3000)) {
//Remote host is online
} else {
//Remote host is offline
}
答案 2 :(得分:0)
使用QT的QNetworkAccessManager类,该类提供networkAccessibleChanged信号 并连接一个插槽
connect(&networkAccessManager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(onNetworkAccessibilityChanged(QNetworkAccessManager::NetworkAccessibility)));
它会在断开以太网电缆时发出信号,告诉您