|
Home > Archive > Unix Programming > February 2006 > Ping from a C/C++ program doesn't work properly
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Ping from a C/C++ program doesn't work properly
|
|
|
| I am trying to find out whether remote host is connected or not using
C/C++ code on Redhat Linux. If its connected, I can send a result to
"Pass" and if its not "Failed"
I tried 2 different things:
1)
#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);
But, even if ethernet cable is connected or not to the remote host, I
always get "Ping_ret" = 0. So, I don't get a desired result in this
case.
2)
I also tried popen():
char psBuffer[1024];
FILE * fdRTP;
if( (fdRTP = popen( "ping -c 1 192.168.0.2", "r" )) == NULL )
{
FSLog(LOG_ERROR, "AebEthernetTest::HandleDmseCommand(): Failed to
create Pipe");
error_code = AEB_DPI_INIFILEREADERR;
}
else
{
/* Read this pipe and print to the stdout until end of file */
while( !feof( fdRTP ) )
{
if( fread( psBuffer, sizeof(char), 1024, fdRTP ) )
{
if(0 == strcmp(psBuffer,"0% loss"))
{
result = TRUE;
}
}
}
}
/* Close pipe */
pclose( fdRTP );
But, in this case, its not even "Ping".
If possible, help me guys.
Thanks
| |
| Pascal Bourguignon 2006-02-14, 5:54 pm |
| "Neel" <nilayhshah@gmail.com> writes:
> I am trying to find out whether remote host is connected or not using
> C/C++ code on Redhat Linux. If its connected, I can send a result to
> "Pass" and if its not "Failed"
>
> I tried 2 different things:
> [...]
> status = system("ping -w 2 192.168.0.2");
I'd add -q:
ping -q -w 2 192.168.0.2
In the shell, ping exit status correctly reports the result.
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| Ian Collins 2006-02-14, 5:54 pm |
| Neel wrote:
> I am trying to find out whether remote host is connected or not using
> C/C++ code on Redhat Linux. If its connected, I can send a result to
> "Pass" and if its not "Failed"
>
> I tried 2 different things:
> 1)
>
> #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);
>
> But, even if ethernet cable is connected or not to the remote host, I
> always get "Ping_ret" = 0. So, I don't get a desired result in this
> case.
>
Well it works on Solaris (without the -w 2), what does the Linux man
page say for the return value of ping?
> 2)
> I also tried popen():
>
Please avoid posting tabs in code, it's hard to read.
> char psBuffer[1024];
> FILE * fdRTP;
>
>
> if( (fdRTP = popen( "ping -c 1 192.168.0.2", "r" )) == NULL )
> {
> FSLog(LOG_ERROR, "AebEthernetTest::HandleDmseCommand(): Failed to
> create Pipe");
> error_code = AEB_DPI_INIFILEREADERR;
> }
>
> else
> {
> /* Read this pipe and print to the stdout until end of file */
> while( !feof( fdRTP ) )
> {
> if( fread( psBuffer, sizeof(char), 1024, fdRTP ) )
> {
What do you see if you print psBuffer here.
--
Ian Collins.
| |
| Ian Collins 2006-02-14, 5:54 pm |
| Neel wrote:
> /* Read this pipe and print to the stdout until end of file */
> while( !feof( fdRTP ) )
> {
> if( fread( psBuffer, sizeof(char), 1024, fdRTP ) )
I didn't spot this before, but the above should be:
fgets( psBuffer, 1024, fdRTP )
--
Ian Collins.
|
|
|
|
|