#include #include #include #include #include #include int main () { int x = 0; int result = 0; int errors = 0; while (x < 1000) { result = openAndClose (); if (result == 0) { printf (" - %d - Connection OK\n", x); } else if (result == 1) { printf (" - %d - Connection FAILED - could not resolve hostname\n", x); errors++; } else if (result == 2) { printf (" - %d - Connection FAILED - could not open socket\n", x); errors++; } else { printf (" - %d - Connection FAILED - due to some reason\n", x); errors++; } x++; usleep (100000); } printf ("ERRORS : %d\n", errors); return; } int openAndClose () { int sockfd; struct hostent *he; struct sockaddr_in server; // Fetch IP - lfsforum.net is a freebsd box - lfs.net is redhat - should not make a difference though if ((he = gethostbyname ("lfsforum.net")) == NULL) //if ((he = gethostbyname ("lfs.net")) == NULL) return 1; // Create socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) return 2; // Prepare connection memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length); server.sin_family = AF_INET; server.sin_port = htons(80); // Open connection if (connect (sockfd, (struct sockaddr *)&server, sizeof(server))) return 3; printf ("CONNECTED TO %u.%u.%u.%u", he->h_addr_list[0][0], he->h_addr_list[0][1], he->h_addr_list[0][2], he->h_addr_list[0][3]); // Close connection close (sockfd); return 0; }