views:

877

answers:

2

Hi,

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.

+3  A: 

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.

Idan K
just to complete my point... im on linux.
ashishsony
Thanks daniel... that was the exact error now i am getting the correct errorcode... Thanks a ton once again.. :)
ashishsony
daniel... the error was the way i was filling up the params QStringList.. u were correct as it was required to insert it as seperate parameters..params << "1.1.1.11" << "-i" << "1" << "-w" << "1" << "-c" << "1"; please re-edit to include that line in ur answer..and by the way i already consulted the ping man page...what error code to search for was clear to me...just i wasnt getting the reqd codes correctly..
ashishsony
oh, I thought at first it wouldn't make a difference and didn't have the time to check it myself, guess I was wrong. Thanks for letting me know.
Idan K
A: 

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

Vladiyork