Skip to content

Instantly share code, notes, and snippets.

@l2m2
Created December 8, 2020 02:20
Show Gist options
  • Save l2m2/d87e7f7e408523cbf1d1cf430786cf8c to your computer and use it in GitHub Desktop.
Save l2m2/d87e7f7e408523cbf1d1cf430786cf8c to your computer and use it in GitHub Desktop.
Qt测试IP是否可达
#include <QCoreApplication>
#include <QtCore>
bool ping_test(const QString &ip) {
QStringList parameters;
#ifdef Q_OS_WIN
parameters << "-n" << "1";
#else
parameters << "-c 1";
#endif
parameters << ip;
int exitCode = QProcess::execute("ping", parameters);
return (exitCode == 0);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << ping_test("10.21.2.65");
qDebug() << ping_test("12.45.33.33");
qDebug() << "Done!";
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment