ramesh Familiar Face
Joined: 16 Nov 2006 Posts: 40
|
Posted: Wed Mar 07, 2007 1:52 pm Post subject: how to know server is down |
|
|
hi all
this is the code to know wether the net on or off
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
int can_resolve_host (const char *host)
{
struct hostent *h = NULL;
h = gethostbyname (host);
if (h == NULL)
{
return -1;
}
else
{
return 0;
}
}
int netstate()
{
FILE* FD;
char buf[256] = {0};
char* linkstatus = NULL;
FD = popen ("/sbin/mii-tool", "r");
if (FD)
{
fgets (buf, sizeof (buf), FD);
buf[strlen(buf) - 1] = '\0';
linkstatus = strstr (buf, "ok");
}
pclose (FD);
if ((linkstatus) && (0 == strcmp (linkstatus, "ok")))
{
if (can_resolve_host ("axillsearch.com") == -1)
{
printf ("Failed to reach axillsearch.com\n");
}
else
{
printf ("Successfully reached axillsearch.com\n");
}
}
else
fprintf (stderr, "%s(): Network connectivity not available \n", __func__);
printf ("U r net is down \n");
return -1;
}
int
main (int argc, char *argv[])
{
int i;
i=gtk_timeout_add(8000,
(GtkFunction) netstate,
NULL);
gtk_main ();
return 0;
}
save it has one.c
and compile
gcc one.c -o one `pkg-config gtk+-2.0 --cflags --libs`
it is working successfully
when
1.net deactivate or activate
2.cable unplugged or plugged
but it is not working when the server is down
how can i do when the server is down
when the serveris down(main servers)
it has to show the message like
"U r net is down \n"
if any help
cheers |
|