使用 Qpr​​ocess 运行 ping,如果主机可达或不可达,则退出代码始终为 2

发布于 2024-07-26 03:46:57 字数 1743 浏览 8 评论 0原文

我正在使用 Qpr​​ocess 执行 ping 来检查主机是否在线...

问题是,我从 Qprocess->finished 信号收到的退出代码始终为 2,无论我 ping 可到达的主机还是一个无法访问的..

我不断地在 QTimer 中 ping 到一台主机(我在运行 Qt 应用程序的客户端上安装了该主机的一个文件夹)...

当我在连接到 QProcess 的插槽中捕获由 ping 返回的退出代码时-> 完成信号.. 我总是收到退出代码为 2..

我无法通过 system(ping) 使用直接系统调用,因为它会在 ping 处于活动状态时挂起我的应用程序... 我希望它是异步的,所以我切换到 QProcess...

以下是代码片段:

//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

无论 1.1.1.1 被 ping 还是 1.1.1.11 被 ping, 行始终给出 2,0 作为输出 终端 1.1.1.1 是可 ping 的,而 1.1.1.11 则不是(我通过在按键事件上设置的 dBool 标志切换 bw ips 以模拟在线/离线主机,以便我的应用程序可以相应地运行)

任何输入都会有很大帮助。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情丝乱 2024-08-02 03:46:57

我认为依赖 ping.exe 退出代码是一个不好的做法,因为它没有记录。 此外,众所周知,在不同版本的 Windows 中,退出代码是不一致的。

您可以:

  • 实施您自己的 ping。 有很多免费的实现,例如 this (在谷歌中搜索“ping.c”时的第一个)。
  • 解析 ping.exe 输出并确定 ping 是否成功。

编辑:

没有意识到您正在使用Linux(下次在您的问题中提及它可能会更明智)...

在将参数发送到 ping 时尝试此操作:

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

而不是一个大字符串。

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.

公布 2024-08-02 03:46:57

没有一个好的跨平台方法可以做到这一点。 但您可以使用特定于平台的方式。 您可以使用以下命令在 Windows 和 Linux 上 ping 通:

#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
}

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
}
反目相谮 2024-08-02 03:46:57

您可以使用 ping->execute (return int) 而不是 ping->start。
这个对我有用 !!!

弗拉迪约克

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

Vladiyork

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文