0

I am having an issue while pinging to Destination / local IP using QProcess. QProcess returns "0" when ping is Successfull while Also when ping gives "Network is Unreachable" error (seen manually on terminal).

Actually I need to check the network connection in my application. To implement it, i used a very simple concept of ping using QProcess. I need to pop up a messagebox if there is any network error otherwise application starts normally without any popup message.

Here is my code for ping :

   bool App::pingPcuStatus(void)
   {
     const QString IP ="192.168.49.44";
     bool ret = false;
     int status;

     if ( IP == "0.0.0.0") {
        status = 1;
     }
    else {
            QProcess pingProcess;
            QString exec = "ping";
            QStringList params;
            params << "-c" << "1" << IP;
            status = pingProcess.execute(exec, params); //status = 0 , if Ping Successfull
            pingProcess.close();
     }

    ret = (status) ? false : true;
    return ret;
    }

But while debugging I found, it returns "true" even i am not conected to network. (I tried manual ping with same command on terminal, it gives "Network is Unreachable" error).

This leads to a big bug in my case.

What is the best way to capture this error using QProcess or any other way to resolve it ???

Any Idea or Suggestion are welcome.

alk
  • 69,737
  • 10
  • 105
  • 255
skg
  • 948
  • 2
  • 19
  • 35
  • I would suggest just using a socket – gvd Dec 06 '12 at 07:20
  • 1
    `execute` is a static method. I don't understand why you are using an object. Ping does not return `0` when the network is unreachable... – UmNyobe Dec 06 '12 at 08:21
  • @UmNyobe Thanks!!! i did'nt noticed about static method. Actually its not been along time for me to start with Qt. While I am still debugging my Code. May be it might be my mistake. As Ping should not return '0'. – skg Dec 06 '12 at 08:31
  • Well if it returns `0` on success, what does it return on failure? Use that.... – Lightness Races in Orbit Dec 06 '12 at 09:28

1 Answers1

1

Original answer from https://stackoverflow.com/a/2148360/793796:

QProcess pingProcess;
QString exec = "ping";
QStringList params;
params << "-c" << "1" << IP;
pingProcess.start(exec,params,QIODevice::ReadOnly);
pingProcess.waitForFinished(-1);

QString p_stdout = pingProcess.readAllStandardOutput();
QString p_stderr = pingProcess.readAllStandardError();

Then you can parse stdout & stderr.

Community
  • 1
  • 1
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • Thanks for your answer. Well i dont know why p_stdout and p_stderr is giving empty string everytime. – skg Dec 06 '12 at 08:35
  • 1
    For now, can you remove `-c 1` from the command line & see if the ping command is running with proper arguments? check ps for your qt process & then check its child process pid. then check `/proc/$childPid/cmdline` to verify, that the command being executed is proper. Also try adding open mode (see edit). – anishsane Dec 06 '12 at 09:36
  • Getting Empty String. – Shaiful Islam Jul 27 '23 at 05:47