/* Example of connection to test if internet enabled :-) * If the internet isn't on you're in real trouble! * * Compile using * * gcc -Wall -Wshadow -g -O0 -lbind -lsocket connect_test.c -o connect_test * * Enjoy! */ #include #include #include #include #include int main(int argc, char *argv[]) { int sock = -1; int ck = 0; struct sockaddr_in sa; /* The struct sockaddr_in is the ipv4 version of the generic * sockaddr structure. We set it up here to point at port 80 * of a www.google.com ip address (216.239.59.104). * Once setup we then use it later in our connect call. * NB the port number must be tranformed into NETWORK ORDER using * htons (for a short) and the ip address (using htonl as it's * a 32-bit value - long). */ memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(80); sa.sin_addr.s_addr = htonl(0xd8ef3b68); /* Try to open a socket */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { printf("Doh! no socket!\n"); exit(-1); } /* Now try to connect. We don't need to talk, just connect. */ ck = connect(sock, (struct sockaddr *)&sa, sizeof(sa)); if (ck != 0) printf("Unable to connect to the http server on 216.239.59.104 [www.google.com]\n"); else printf("Guess the internet is on at the moment\n"); /* Close our socket, just to be tidy */ close(sock); /* Return that we're all done... */ return 0; }