Commit 7a9cb116 by “liusq”

修改命令bug1

parent fa9c2024
...@@ -484,6 +484,8 @@ inline bool GetNetworkInfoByQNetworkInterface(QString &mac, QString &subnetMask, ...@@ -484,6 +484,8 @@ inline bool GetNetworkInfoByQNetworkInterface(QString &mac, QString &subnetMask,
} }
inline bool pingAddress(const QString &address) { inline bool pingAddress(const QString &address) {
QProcess cmd;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// Windows 指令 "ping IP -n 1 -w 超时(ms)" // Windows 指令 "ping IP -n 1 -w 超时(ms)"
QString cmdstr = QString("ping %1 -n 1 -w %2") QString cmdstr = QString("ping %1 -n 1 -w %2")
...@@ -494,27 +496,38 @@ inline bool pingAddress(const QString &address) { ...@@ -494,27 +496,38 @@ inline bool pingAddress(const QString &address) {
.arg(address); .arg(address);
#endif #endif
QProcess cmd; // 启动进程
cmd.start(cmdstr); cmd.start(cmdstr);
cmd.waitForReadyRead(1000);
cmd.waitForFinished(1000);
QString res = QString::fromLocal8Bit(cmd.readAll()); // 等待进程准备好读取
qInfo() << res; if (!cmd.waitForStarted()) {
qWarning() << "Failed to start 'ping' process for" << address;
return false;
}
#ifdef Q_OS_WIN // 等待进程完成
if (res.indexOf("TTL") == -1) if (!cmd.waitForFinished(3000)) { // 增加超时时间
#else cmd.kill();
if (!res.contains("1 packets transmitted, 1 received")) qWarning() << "Ping process timed out for" << address;
#endif
{
qInfo() << address << QString::fromLocal8Bit("ping不通");
return false; return false;
} }
else
{ // 读取输出
qInfo() << address << QString::fromLocal8Bit("ping通"); QByteArray output = cmd.readAllStandardOutput();
QByteArray errorOutput = cmd.readAllStandardError();
int exitCode = cmd.exitCode();
qInfo() << "Ping Output for" << address << ":" << QString::fromUtf8(output);
qInfo() << "Ping Error Output for" << address << ":" << QString::fromUtf8(errorOutput);
qInfo() << "Ping Exit Code for" << address << ":" << exitCode;
// 判断 Ping 是否成功
if (exitCode == 0) {
qInfo() << address << "ping通";
return true; return true;
} else {
qInfo() << address << "ping不通";
return false;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment