系统(“Ping”)总是返回0 [英] System("Ping") always returns 0

查看:422
本文介绍了系统(“Ping”)总是返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试ping我的C ++ / Redhat Linux代码中的远程主机

检查主机是否已连接。


if(0 == system(" ping) -w 2 192.168.0.2))


但是,在这两种情况下(连接/断开连接),系统调用返回0.可以

有人请显示我的内容我做错了吗?如何在Redhat linux上使用C ++查看ping状态的实际结果?


提前致谢。

I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?

Thanks in advance.

推荐答案

Neel写道:
我正在尝试ping我的C ++ / Redhat Linux代码中的远程主机,用于检查主机是否已连接。

if(0 == system(" ping -w 2 192.168.0.2))

但是,在这两种情况下(连接/断开连接),系统调用返回0.可以
某人请说明我做错了什么?如何在Redhat linux上使用C ++检查ping状态的实际结果?
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?



重新发布到comp.unix.programmer。


< OT>系统返回分叉命令的结果,而不是命令

返回< OT>


-

Ian Collins。


Repost to comp.unix.programmer.

<OT>system returns the result of forking the command, not the command
return<OT>

--
Ian Collins.


Neel写道:
我正在尝试ping我的C ++ / Redhat Linux代码中的远程主机,用于检查主机是否已连接。

if(0 == system(" ping -w 2 192.168.0.2))

但是,在这两种情况下(连接/断开连接),系统调用返回0.可以
某人请说明我做错了什么?如何在Redhat linux上使用C ++检查ping状态的实际结果?

提前致谢。
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.

if (0 == system("ping -w 2 192.168.0.2))

But, in both cases (connected/disconnected), system call returns 0. Can
someone please show what I am doing wrong? How to check the actual
result of the ping status using C++ on Redhat linux ?

Thanks in advance.




这里' ''man system''的剪辑:


[snip]

返回值

返回的值为-1出错时(例如fork失败),否则返回

状态。后一种返回状态采用wait(2)中指定的

格式。因此,该命令的退出代码将是

为WEXITSTATUS(状态)。如果/ bin / sh无法执行,

退出状态将是退出(127)的命令。

[snip]


所以,


#include< stdlib.h>

#include< sys / types.h>

#include< sys / wait.h>


int ping_ret,status;


status = system(" ; ping -w 2 192.168.0.2");


if(-1!= status)

ping_ret = WEXITSTATUS(status);


问候,

拉里



Here''s a snip from ''man system'':

[snip]
RETURN VALUE
The value returned is -1 on error (e.g. fork failed), and the return
status of the command otherwise. This latter return status is in the
format specified in wait(2). Thus, the exit code of the command will
be WEXITSTATUS(status). In case /bin/sh could not be executed,
the exit status will be that of a command that does exit(127).
[snip]

So,

#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int ping_ret, status;

status = system("ping -w 2 192.168.0.2");

if (-1 != status)
ping_ret = WEXITSTATUS(status);

Regards,
Larry


非常感谢拉里的帮助。


BUT,

正如我所说,我想知道ping结果(如果我从远程主机获得ping回复

,则为TRUE)和(如果我没有收到来自

远程主机的ping回复,则为FALSE)。


我使用WEXITSTATUS(状态)与远程主机断开连接。我仍然是获得一个真正的
。请看下面我的代码。


sprintf(pingstring,ping -w 2%s,ip_add);


//执行''ping''命令通过系统调用

status = system(pingstring); // lint!e421函数''系统(const

char *)''被认为是危险的[MISRA规则126]


PDEBUG("返回状态:%d",status);


if(-1!= status)

{

ping_ret = WEXITSTATUS(状态);


PDEBUG(Ping WEXITSTATUS ping_ret:%d,ping_ret);


if(0 == ping_ret)

{

result = TRUE;

}

}


在两种情况下,当远程主机回复或没有回复时,我得到

ping_ret作为0.


让我知道我在这里做错了什么。

感谢这个愚蠢的问题

Thanks a lot Larry for help.

BUT,
as I said, I would like to know the ping result (If I get a ping reply
from remote host, its TRUE) and (if I don''t get a ping reply from
remote host, its FALSE).

I used WEXITSTATUS(status) with remote host disconnected. I am still
getting a TRUE. Please see below my code.

sprintf(pingstring,"ping -w 2 %s",ip_add);

// Execute ''ping'' command via the system call
status = system(pingstring); //lint !e421 function ''system(const
char *)'' is considered dangerous [MISRA Rule 126]

PDEBUG("Ping returned status:%d", status);

if (-1 != status)
{
ping_ret = WEXITSTATUS(status);

PDEBUG("Ping WEXITSTATUS ping_ret:%d", ping_ret);

if(0 == ping_ret)
{
result = TRUE;
}
}

In both cases when remote host replys or doesn''t reply, i get
"ping_ret" as 0.

Let me know what i am doing wrong here.
Thanks for this stupid question


这篇关于系统(“Ping”)总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆