Question

i am using Qprocess to execute ping to check for a host to be online or not...

The problem is that the exit code that i recieve from the Qprocess->finished signal is always 2 no matter if i ping a reachable host or an unreachable one..

I am continuously pinging in a QTimer to a host(whose one folder i have mounted at client where the Qt app is running)...

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

i cant use direct system call through system(ping) as it hangs my app for the time ping is active... i want it to be asynchronous so i switched to QProcess...

the following is the code snippet:

//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";
    }
}   

the

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

line always gives 2,0 as output no matter if 1.1.1.1 is pinged or 1.1.1.11 is pinged on terminal 1.1.1.1 is pingable and 1.1.1.11 is not (i switch bw ips through dBool flag that is set on keypress event to simulate online/offline host so that my app can behave accordingly)

Any inputs would be a great help..

Thanks.

Was it helpful?

Solution

I think it's a bad practice to rely on ping.exe exit code as it's undocumented. Furthermore it's been known that in different versions of Windows the exit code is inconsistent.

You could:

  • implement your own ping. there are plenty free implementations out there such as this (first one when searching "ping.c" in google).
  • parse ping.exe output and determine if the ping was successful or not.

EDIT:

Didn't realize you're working with Linux (next time it might be wiser to mention it in your question)...

Try this when sending the arguments to ping:

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

instead of one big string.

OTHER TIPS

There isn't a good cross platform way for doing this. But you can use platform specific ways. You can ping on both Windows and Linux using this:

#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's alive
} else 
{
    // it's dead
}

You can use ping->execute (return int) instead of ping->start. It works for me !!!

Vladiyork

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top