Running ping with Qprocess, exit code is always 2 if host is available or not

I use Qprocess to ping to check if the host is connected to the network or not ...

The problem is that the exit code that I get from Qprocess-> the finished signal is always 2, regardless of whether I can ping an available host or an inaccessible one.

I constantly ping in QTimer to the host (whose one folder I installed on the client where the Qt application works) ...

when I catch the exit code returned by ping in the slot connected to QProcess-> the finished signal. I always get the exit code as 2.

I cannot use a direct system call through the system (ping) because it hangs by my application for the time when ping is active ... I want it to be asynchronous, so I switched to QProcess ...

The following is a snippet of code:

//Pinging function called inside a timer with timout 1000        
QString exec="ping";
        QStringList params;
        if(!dBool)
        {
            //params << "-c1 1.1.1.11 -i1 -w1;echo $?";
            params <<" 1.1.1.11 -i 1 -w 1 -c 1";//wont ping
            cout<<"\n\npinging 11 ie wont ping";
        }
        else
        {
            //params << "-c1 1.1.1.1 -i1 -w1;echo $?";
            params <<" 1.1.1.1 -i 1 -w 1 -c 1";//will ping
            cout<<"\n\npinging 1 ie will ping";
        }
        ping->start(exec,params);
// the slot that connects with QProcess->finished signal
void QgisApp::pingFinished( int exitCode, QProcess::ExitStatus exitStatus )
{
    cout<<"\n\nexitCode,exitStatus=="<<exitCode<<","<<exitStatus;//always 2,0!!
    if(exitCode==0)
    //if(dBool)
    {
        connectivity=true;
        cout<<"\n\nONLINE";
    }
    else
    {
        connectivity=false;
        cout<<"\n\nOFFLINE";
    }
}   

cout<<"\n\nexitCode,exitStatus=="<<exitCode<<","<<exitStatus
Line

always gives 2.0 as an output, regardless of whether 1.1.1.1 is written or 1.1.1.11 is written on the terminal 1.1.1.1 is pingable and 1.1.1.11 is not (i switches bw ips via the dBool flag that is set to the keypress event to simulate an online host so that my application can behave accordingly)

Any inputs would be a big help.

Thanks.

+3
source share
3 answers

I find it a bad practice to rely on the ping.exe exit code as it is undocumented. In addition, it was known that in different versions of Windows, the exit code is incompatible.

You can:

  • . , this ( "ping.c" google).
  • parse ping.exe , ping .

EDIT:

, Linux ( )...

ping:

params << "1.1.1.11" << "-i" << "1" << "-w" << "1" <<"-c" <<"1";

.

+6

- . . ping Windows, Linux, :

#if defined(WIN32)
   QString parameter = "-n 1";
#else
   QString parameter = "-c 1";
#endif

int exitCode = QProcess::execute("ping", QStringList() << parameter << "1.1.1.11");
if (exitCode==0) 
{
    // it alive
} else 
{
    // it dead
}
+1

ping- > execute (return int) ping- > start. !

Vladiyork

0

Source: https://habr.com/ru/post/1711394/


All Articles