问题描述
我有一个问题,而使用QProcess中侦测到目标地址/本地IP。另外,QProcess返回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).
其实我需要检查我的应用程序的网络连接。为了实现它,我用使用QProcess中的ping一个非常简单的概念。我需要弹出一个消息框,如果有任何网络错误,否则应用程序正常启动,没有任何弹出消息。
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.
下面是我的code为平:
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;
}
不过,虽然我调试发现,就返回真即使我没有conected网络。 (我试过与终端相同的命令手动平,它给网络不可达的错误)。
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.
什么是使用QProcess中或任何其他方式来解决它?抓住这个错误的最好办法
What is the best way to capture this error using QProcess or any other way to resolve it ???
任何意见或建议,欢迎。
Any Idea or Suggestion are welcome.
推荐答案
原装:
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.
这篇关于如何检测&QUOT;网络不可达&QUOT;通过使用平QProcess中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!