Ping from a C/C++ program doesn't work properly
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Ping from a C/C++ program doesn't work properly




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Ping from a C/C++ program doesn't work properly  
Neel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-14-06 10:54 PM

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






[ Post a follow-up to this message ]



    Re: Ping from a C/C++ program doesn't work properly  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-14-06 10: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





[ Post a follow-up to this message ]



    Re: Ping from a C/C++ program doesn't work properly  
Ian Collins


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-14-06 10: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.





[ Post a follow-up to this message ]



    Re: Ping from a C/C++ program doesn't work properly  
Ian Collins


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-14-06 10: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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:57 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register