windows 如何在 C++ 中捕获 ping 的返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1570364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capture ping's return value in C++
提问by kampi
Does anyone know, how to capture ping's return value in c++? According to this link:
ping should return 0
on success, 1
on failure such as unknown host, illegal packet size, etc. and 2
On a unreachable host or network.
有谁知道,如何在 C++ 中捕获 ping 的返回值?根据此链接:ping 应0
在成功、1
失败(例如未知主机、非法数据包大小等)以及2
无法访问的主机或网络上返回。
In C++ I called ping with the system ()
, e.g. int ret = system("ping 192.168.1.5");
.
在 C++ 中,我用 调用了 ping system ()
,例如int ret = system("ping 192.168.1.5");
。
My problem is, that ret
's value is 0
or 1
. It will never 2
! If I think correctly, this is because, this return value I get, is the system functions return value, not ping's. So how could i get ping's return vlaue?
我的问题是,它ret
的值是0
or 1
。它永远不会2
!如果我认为正确,这是因为,我得到的这个返回值是系统函数的返回值,而不是 ping 的。那么我怎么能得到 ping 的返回值呢?
Thanks in advance!
提前致谢!
kampi
坎皮
Edit: Right now i use this system("ping 192.169.1.5 > ping_res.txt"); but i don't want to work with files (open, and read them), that's why i want to capture, th return value , if possible :)
编辑:现在我使用这个系统(“ping 192.169.1.5 > ping_res.txt”);但我不想处理文件(打开并读取它们),这就是为什么我想捕获返回值,如果可能的话:)
回答by cmeerw
If you are on Windows, it might be better to use IcmpSendEcho2directly to implement the ping functionality in your application.
如果您使用的是 Windows,最好直接使用IcmpSendEcho2在您的应用程序中实现 ping 功能。
回答by Jacob
A simple solution would be pipe the output of ping to to a file and read it.
一个简单的解决方案是将 ping 的输出通过管道传输到文件并读取它。
E.g.
例如
system("ping 192.169.1.5 > ping_res.txt");
system("ping 192.169.1.5 > ping_res.txt");
And read ping_res.txt
to get the info you need.
并阅读ping_res.txt
以获取您需要的信息。
回答by Heath Hunnicutt
From man 3 system
on Linux:
从man 3 system
Linux上:
RETURN VALUE
The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2).
返回值
错误返回值是 -1(例如 fork(2) 失败),否则返回命令的状态。后一种返回状态采用wait(2) 中指定的格式。
Then from man 2 wait
on Linux:
然后man 2 wait
在 Linux 上:
If status is not NULL, wait() and waitpid() store status information in the int to which it points. This integer can be inspected with the following macros (which take the integer itself as an argument, not a pointer to it, as is done in wait() and wait- pid()!):
WIFEXITED(status)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().WEXITSTATUS(status)
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.
如果 status 不是 NULL,wait() 和 waitpid() 将状态信息存储在它指向的 int 中。可以使用以下宏检查这个整数(它们将整数本身作为参数,而不是指向它的指针,就像在 wait() 和 wait-pid() 中所做的那样!):
如果子进程正常终止,即通过调用 exit(3) 或 _exit(2),或者从 main() 返回,则WIFEXITED(status)返回 true。WEXITSTATUS(status)
返回子进程的退出状态。这由子进程在调用 exit(3) 或 _exit(2) 或作为 main() 中 return 语句的参数指定的状态参数的最低有效 8 位组成。仅当 WIFEXITED 返回 true 时才应使用此宏。
From sys/wait.h on Linux:
来自 Linux 上的 sys/wait.h:
# define WEXITSTATUS(status) __WEXITSTATUS(__WAIT_INT(status))
From bits/waitstatus.h on Linux:
来自 Linux 上的 bits/waitstatus.h:
/* If WIFEXITED(STATUS), the low-order 8 bits of the status. */ #define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
In other words, you will wan to use these macros if you are using Linux. Are you using HP-UX? I notice you link to information for HP-UX. If so, what does your man 3 system
page say?
换句话说,如果您使用的是 Linux,您将需要使用这些宏。您在使用 HP-UX 吗?我注意到您链接到 HP-UX 的信息。如果是这样,你的man 3 system
页面说的是什么?
Also, keep in mind that system
invokes "sh -c command" and you will receive the return value of sh:
另外,请记住,system
调用“sh -c命令”,您将收到 sh 的返回值:
EXIT STATUS
The following exit values shall be returned:
0 The script to be executed consisted solely of zero or more blank lines or comments, or both.
1-125 A non-interactive shell detected a syntax, redirection, or variable assignment error.
127 A specified command_file could not be found by a non-interactive shell.
Otherwise, the shell shall return the exit status of the last command it invoked or attempted to invoke (see also the exit utility in Special Built-In Utilities).
退出状态
应返回以下退出值:
0 要执行的脚本仅由零个或多个空行或注释或两者组成。
1-125 非交互式 shell 检测到语法、重定向或变量赋值错误。
127 非交互式 shell 找不到指定的 command_file。
否则,shell 应返回它调用或尝试调用的最后一个命令的退出状态(另请参阅特殊内置实用程序中的退出实用程序)。
What return value do you find if you attempt, e.g., system("exit 203");
?
如果您尝试,例如,您会发现什么返回值system("exit 203");
?
回答by ercan
So you just want to know the return value: i.e. "0 on success, 1 on failure such as unknown host, illegal packet size, etc. and 2 On a unreachable host or network." I think using ping is an overkill in this case. Writing to file and reading from it is apparently a little bit ugly.
因此,您只想知道返回值:即“成功时为 0,失败时为 1,例如未知主机、非法数据包大小等。以及 2 在无法访问的主机或网络上。”我认为在这种情况下使用 ping 是一种矫枉过正. 写入文件并从中读取显然有点难看。
I think you should just try **open()**ing a connection to the host on port 7 (Echo). Then you would get the information that ping return value would tell you. If you further want to get response durations, you can measure it yourself by sending arbitrary data to the host on echo port. It would take some time to write the code but i think it's worth for flexibility and accuracy.
我认为您应该尝试 **open()** 连接到端口 7 (Echo) 上的主机。然后你会得到 ping 返回值会告诉你的信息。如果您还想获得响应持续时间,您可以通过在 echo 端口上向主机发送任意数据来自行测量。编写代码需要一些时间,但我认为它的灵活性和准确性是值得的。