使用 Qprocess 运行 ping,如果主机可达或不可达,则退出代码始终为 2
我正在使用 Qprocess 执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为依赖 ping.exe 退出代码是一个不好的做法,因为它没有记录。 此外,众所周知,在不同版本的 Windows 中,退出代码是不一致的。
您可以:
编辑:
没有意识到您正在使用Linux(下次在您的问题中提及它可能会更明智)...
在将参数发送到 ping 时尝试此操作:
而不是一个大字符串。
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:
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:
instead of one big string.
没有一个好的跨平台方法可以做到这一点。 但您可以使用特定于平台的方式。 您可以使用以下命令在 Windows 和 Linux 上 ping 通:
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:
您可以使用 ping->execute (return int) 而不是 ping->start。
这个对我有用 !!!
弗拉迪约克
You can use ping->execute (return int) instead of ping->start.
It works for me !!!
Vladiyork