Get the ping from a remote target with Qt (Windows/Linux)

前端 未结 4 2715
不知归路
不知归路 2021-02-20 17:19

Currently I use this code for retrieving the ping of a target system. However it works so far only under linux and it is likely dependent on the locale settings. To add suppor

相关标签:
4条回答
  • 2021-02-20 17:30

    You would need write your code for this without QAbstractSocket. In short, this base class was not designed for this use case.

    The reason is that you would need to use raw sockets and run as root; that is also why you usually see the setuid flag set on the ping executable on Linux.

    ICMP is "connectionless", and as such the Qt class is not feasible for it as it does not provide sending data to a specific host, etc.

    You can read the more thorough explanation in here.

    0 讨论(0)
  • 2021-02-20 17:40

    Nejat's code didn't work for me neither. Maybe it's Windows specific (tested on Windows 7 x64 + Qt 5.6). Ping command seems to distinct parameters and values and needs them to be passed apart when creating QProcess.

    So instead of passing "-n 1" in one go, you'll need to pass "-n" and "1" separately.

    Basically, with Nejat's code it would be:

    int exitCode = QProcess::execute("ping", QStringList()
                                         << "-n" << "1"
                                         << m_sHostName);
    if (exitCode==0) {
        // it's alive
    } else {
        // it's dead
    }
    

    Tested and working now.

    0 讨论(0)
  • 2021-02-20 17:45

    You can ping on both Windows and Linux using this:

       QStringList parameters;
    #if defined(WIN32)
       parameters << "-n" << "1";
    #else
       parameters << "-c 1";
    #endif
    
       parameters << m_sHostName;
    
       int exitCode = QProcess::execute("ping", parameters);
       if (exitCode==0) {
           // it's alive
       } else {
           // it's dead
       }
    
    0 讨论(0)
  • 2021-02-20 17:47

    The ping method return a 0 exit code event if the target is unreachable in Windows. You have to parse the command output. The following example is working on Windows and linux :

    class MyClass {
      private slots:
       void OnPing();
       void OnPingEnded();
      private:
       Process mPingProcess;
    };
    
    void MyClass::OnPing()
    {
       connect(&mPingProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(OnPingEnded()));
    #ifdef __linux__
       mPingProcess.start("ping", QStringList() << "-c" << "1" << ui->ip->text());
    #else
       mPingProcess.start("ping", QStringList() << "-n" << "1" << ui->ip->text());
    #endif
    }
    
    void MyClass::OnPingEnded()
    {
        QByteArray output = mPingProcess.readAllStandardOutput();
        if (!output.isEmpty())
        {
            qDebug() << output;
            if (-1 != QString(output).indexOf("ttl", 0, Qt::CaseInsensitive))
            {
               qDebug() << "PING OK";
            }
            else
            {
               qDebug() << "PING KO";
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题