QPing
-
Ping class for Qt 5 or 6
-
You can check the response by shooting the ping.
Note
- I do not call functions that use the ICMP protocol.
- I have written a raw socket program for pinging in the past.
- But there’s a problem with user’s elevation. Application should be executed by root or superuser.
How to use
-
Copy QPing.h, QPing.cpp to your project.
-
Append Qt project(*.pro) setting of source code
HEADERS += QPing.h
SOURCES += QPing.cpp
- Add source code that applies the class. See main.cpp.
#include <QtGlobal>
#include <QCoreApplication>
#include <QProcess>
#include <QtGlobal>
#include <QDebug>
#include <QFile>
#include <QSettings>
#include <QLocale>
#include <iostream>
#include "QPing.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString destIpAddress = "192.168.200.254";
if ( argc == 2 )
{
// You can use application first parameter
// For example) QPingSF 192.168.0.10
QString strArg = argv[1];
destIpAddress = strArg;
}
// Define your OS and OS language.
// See sample of *.ini for OS. (You can define your own INI file)
// Please suggest strings that can be set in various languages and operating systems.
QString iniFilePath;
QString localSystemName = QLocale::system().name();
/* Returns the language and country of this locale as a string of the form "language_country",
* where language is a lowercase, two-letter ISO 639 language code,
* and country is an uppercase, two- or three-letter ISO 3166 country code.
*/
QString langaugeISO639 = localSystemName.left(2);
#ifdef Q_OS_WIN
if ( langaugeISO639 == "ko" )
{
iniFilePath = ":/ping-config-win-kr.ini"; // Windows, Korean
}
if ( langaugeISO639 == "en" )
{
iniFilePath = ":/ping-config-win-en.ini"; // Windows, English
}
/* TODO:
* Create a new INI file for your native language, referencing an existing INI file.
*/
#endif
#ifdef Q_OS_LINUX
if ( langaugeISO639 == "en" )
{
iniFilePath = ":/ping-config-linux-en.ini"; // Linux, English
}
/* TODO:
* Create a new INI file for your native language, referencing an existing INI file.
*/
#endif
#ifdef Q_OS_MAC
if ( langaugeISO639 == "en" ) // Mac, English
{
// iniFilePath = mac ini... I don't have a Mac, because I'm poor.
}
/* TODO:
* Create a new INI file for your native language, referencing an existing INI file.
*/
#endif
////////////////////
QPing qp; // main class
qp.setIniFile( iniFilePath ); // set configuration file
if ( ! qp.loadIniFile() )
{
std::cout << "[ERROR] Failed to load ini file" << std::endl;
return (-1);
}
// Ping!
QPing::pingResult result = qp.ping(destIpAddress);
switch( result )
{
case QPing::pingSuccess:
std::cout << "[SUCCESS] Success to ping" << std::endl;
break;
case QPing::pingFailed:
std::cout << "[FAILED] Failed to ping" << std::endl;
break;
case QPing::initFailed1: // "success string is empty"
case QPing::initFailed2: // "failed string is empty"
std::cout << "[ERROR] Initialization is failed" << std::endl;
break;
case QPing::notFound: // something wrong
std::cout << "[ERROR] Result is not found" << std::endl;
break;
default:
std::cout << "[ERROR] Undefined result : " << ((quint32)result) << std::endl;
break;
}
return 0;
}
License and links
- QPing is under MIT license. https://github.com/JayTwoLab/QPing
Contact
-
Leave me a issue. https://github.com/JayTwoLab/QPing/issues
-
Hi! I’m j2doll. My native language is not English and my English is not fluent. Please, use EASY English. :-)