user3780183
user3780183

Reputation: 29

Qt QProccess with ping utility usage

I need some help with QProcess class' method called "execute"

I wanna know is server alive using external utility "ping" in windows7. I make:

int exitCode = QProcess::execute(QString("ping -n %1 %2").arg(packetNumber).arg(hostAddr.toString()));
if (exitCode == 0){
    // it's alive
    qDebug() << "It's alive!";
}
else{
    // it's dead
    qDebug() << "It's dead!";
}

External prints into the console some info, that I don't want see. Ideally I want to run my function (part of its body is written upper) in the child thread. Other words, I just want to get "It's dead\alive !" in console from these code lines

Upvotes: 1

Views: 889

Answers (1)

balajeerc
balajeerc

Reputation: 4098

That would require a little more work. You need to treat the QProcess as an IODevice, listening to its readyRead() signal for whenever information is available from it.

Here is the complete code to do what you want:

Pinger.h

#ifndef PINGER_H
#define PINGER_H

#include <QtCore/QTimer>
#include <QtCore/QProcess>

/// \brief  Starts pinging a specified url constantly
///         As soon as ping fails, it emits a 'pingFailed'
///         signal
class Pinger : public QObject
{
Q_OBJECT
public:
    Pinger();
    ~Pinger();

    /// \brief  Starts a ping check in a loop
    /// \param[in]  urlToPing   The url that needs to be pinged continuously
    /// \param[in]  pingInterval    Interval (in seconds) at which specified url must be pinged
    void startPingCheck(const QString& urlToPing, const int& pingIntervalInSecs = 5);

signals:
    /// \brief  Signal emitted when pinging of specified url fails
    void pingFailed();

private slots:
    /// \brief  Slot called periodically to ping specified url
    void _pingExec();

    /// \brief  Slot called when ping process returns a message
    void _handleProcessMessage();

private:
    /// \brief  Timer used to ping repeatedly
    QTimer _pingTimer;

    /// \brief  Ping process
    QProcess _pingProcess;

    /// \brief  Pinged url
    QString _pingUrl;
};

#endif // PINGER_H

Pinger.cpp

#include <QtCore/QDebug>

#include "pinger.h"

Pinger::Pinger():
    _pingUrl("8.8.8.8")
{

}

Pinger::~Pinger()
{

}

void Pinger::startPingCheck(const QString& urlToPing, const int& pingInterval)
{
    _pingUrl = urlToPing;

    // Listen for messages from the ping process
    connect(&_pingProcess, SIGNAL(readyRead()), this, SLOT(_handleProcessMessage()));

    // Connect the timer to the exec method that actually calls the ping process
    _pingTimer.setInterval(pingInterval * 1000);
    _pingTimer.setSingleShot(false);
    connect(&_pingTimer, SIGNAL(timeout()), this, SLOT(_pingExec()));
    _pingTimer.start();
}

void Pinger::_pingExec()
{
    QStringList arguments;
    arguments << "-n" << "1" << _pingUrl;
    _pingProcess.start("ping", arguments);
}

void Pinger::_handleProcessMessage()
{
    QByteArray response = _pingProcess.readAll();
    QString responseStr(response);
    if(responseStr.contains("100% loss"))
    {
        qDebug() << "Ping failed. " << _pingUrl << " down.";
        emit pingFailed();
    }
}

Upvotes: 1

Related Questions