#include #include using namespace std; #include #include #include #include #include #include #include #include #include #include #include static int echo_interval = 1; /*!< Default echo interval is 1 sec */ static int echo_port = 5000; /*!< Default port for echo service */ static string echo_server; /*!< Server name (Panel PC) */ static bool verbose = false; /*!< If true then the program operates in verbose mode */ int main(int argc, char **argv) { verbose = true; echo_server = "PanelPC"; printf ("Starting ......."); /* Establish a connection with the echo server */ struct hostent *he = gethostbyname(echo_server.c_str()); string ip = inet_ntoa(*(struct in_addr*)he->h_addr_list[0]); struct sockaddr_in addr; memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; addr.sin_port = htons (echo_port); memcpy (&addr.sin_addr, he->h_addr_list[0], 4); int rd; int sid; time_t time_of_day; char tx[128]; char rx[128]; while (1) { sid = socket(AF_INET, SOCK_STREAM, 0); if (verbose) printf ("Watchdog: Connecting to host '%s' with ip address %s:%d\n", echo_server.c_str(), ip.c_str(), echo_port); if ( connect (sid, (sockaddr *)&addr, sizeof(addr)) == -1 ) { /* No connection with the server - try again */ fprintf (stderr, "%s, %s: Connection to host=%s:%d failed: %s\n", __FILE__, __FUNCTION__, ip.c_str(), echo_port, strerror (errno)); close(sid); } else { /* Start sending and receiving echo messages */ time_of_day = time(NULL); strftime(tx, 128, "Echo message send by client at %T", localtime(&time_of_day)); send(sid, tx, strlen(tx), 0); while ( (rd = recv(sid, rx, 128, 0)) != -1 ) { if (verbose && (rd > 0) ) printf ("Watchdog: %s returned: %s\n", echo_server.c_str(), rx); sleep (echo_interval); time_of_day = time(NULL); strftime(tx, 128, "Echo message send by client at %T", localtime(&time_of_day)); if ( send(sid, tx, strlen(tx), 0) == -1 ) break; } fprintf (stderr, "%s, %s: Connection to host=%s:%d lossed: %s\n", __FILE__, __FUNCTION__, ip.c_str(), echo_port, strerror (errno)); close(sid); } } return 0; }