Here is a little code that uses liboping to ping www.xively.com every second and displays the delay. You can install liboping static / dynamic library files and header files in your ubuntu field as follows: sudo apt-get install liboping0 liboping-dev oping
Then compile the following program with the above library ( gcc -o test test.c -loping ). And run the executable as root (sudo).
test.c:
/* * 1. install liboping, eg `sudo apt-get install liboping0 liboping-dev oping` * 2. Compile with -loping, eg `gcc -o test test.c -loping` * 3. Execute using sudo as super user, eg `sudo ./test` */ #include <stdlib.h> #include <stdio.h> #include <oping.h> int main(int argc, char **argv) { pingobj_t *ping; pingobj_iter_t *iter; if ((ping = ping_construct()) == NULL) { fprintf(stderr, "ping_construct failed\n"); return (-1); } printf("ping_construct() success\n"); if (ping_host_add(ping, "www.xively.com") < 0) { const char * errmsg = ping_get_error(ping); fprintf(stderr, "ping_host_add(www.xively.com) failed. %s\n", errmsg); return (-1); } printf("ping_host_add() success\n"); while (1) { if (ping_send(ping) < 0) { fprintf(stderr, "ping_send failed\n"); return (-1); } printf("ping_send() success\n"); for (iter = ping_iterator_get(ping); iter != NULL; iter = ping_iterator_next(iter)) { char hostname[100]; double latency; unsigned int len; printf("ping_iterator_get() success\n"); len = 100; ping_iterator_get_info(iter, PING_INFO_HOSTNAME, hostname, &len); len = sizeof(double); ping_iterator_get_info(iter, PING_INFO_LATENCY, &latency, &len); printf("hostname = %s, latency = %f\n", hostname, latency); } sleep(1); } printf("exiting...\n"); ping_destroy( ping ); return 0; }
Output:
anurag@anurag-PC :~$ sudo ./test ping_construct() success ping_host_add() success ping_send() success ping_iterator_get() success hostname = www.xively.com, latency = 233.666000 ping_send() success ping_iterator_get() success hostname = www.xively.com, latency = 234.360000 ping_send() success ping_iterator_get() success hostname = www.xively.com, latency = 234.076000 ping_send() success ping_iterator_get() success hostname = www.xively.com, latency = 231.761000 ping_send() success ping_iterator_get() success hostname = www.xively.com, latency = 235.085000 ^C
liboping is good if you want to check the Internet connection from your Linux device, if your ISP or target does not block ICMP packets. If they are blocked, you can use some HTTP library to try to extract the index.html page from www.google.com or any other website and see if it succeeded.
lithiumhead
source share