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 ]
|