Results 1 to 7 of 7
can anybody please tell me how to make a program in c/c++ which checks the internet connection in every 15 mins and runs in a background and also can be ...
- 05-19-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 5
c/c++ program to check internet connection
can anybody please tell me how to make a program in c/c++ which checks the internet connection in every 15 mins and runs in a background and also can be executed from php script using exec.
- 05-20-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Easier said than done. The problem isn't so much with the code as with how do you determine that the connection to the internet is down vs. a problem such as a site is down and not responding? So, please try to provide more complete information about exactly what you want to do, and more importantly why you need to do it?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-20-2009 #3Linux Newbie
- Join Date
- Sep 2004
- Location
- UK
- Posts
- 160
You don't need C and C++ if that's all your doing, you can do it with a script.
You would need to test against minimum of 2 well know (high uptime) internet sites andyour service provider's router to ensure coverage.
something like (pusedo code):
Code:while(true) internet_up = true if test againt google != success if test against yahoo != success if test against router != success internet_up = false endif endif endif if internet_up = false echo "DOWN" else echo "UP" endif sleep 15mins doneIn a world without walls and fences, who needs Windows and Gates?
- 05-20-2009 #4Just Joined!
- Join Date
- May 2009
- Posts
- 5
I want to control the lpt ports from internet using php (which executes c program on the server to control lpt ports) program and I succeeded in doing that. But I want to add more feature by adding network checker which runs in a background on the server. When the internet connection is down the lpt port status changes and when it is available the status again sets to a previous value. I succeeded doing that using infinite loop in which system call is used to ping the network and returns a value. But I want to know that is there any method other than infinite loop which runs in background and checks the internet connection.
- 05-20-2009 #5Linux Newbie
- Join Date
- Sep 2004
- Location
- UK
- Posts
- 160
In a world without walls and fences, who needs Windows and Gates?
- 05-22-2009 #6
Well, wouldn't you need to worry about time-outs in that case? I'm not sure that the connection will stay open indefinitely.
Anyway, I actually have something that's probably pretty close to what you're looking for...
It's not designed to work in a loop, but I'm sure that you can use it in a script much like you're doing with ping, or if you need to just write a program with that as a function, and put it in a loop and main function that does what you want depending on whatever variable. It should be obvious how it works.Code:/* ** connectip.c -- connect to ip address */ #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h> #include <errno.h> int main(int argc, char *argv[]) { struct addrinfo hints, *res, *p; int status, sockfd; char ipstr[INET6_ADDRSTRLEN]; if (argc != 3) { fprintf(stderr,"usage: connectip hostname port\n"); return 1; } memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version hints.ai_socktype = SOCK_STREAM; if ((status = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 2; } printf("Connection to..."); for(p = res;p != NULL; p = p->ai_next) { void *addr; char *ipver; // get the pointer to the address itself, // different fields in IPv4 and IPv6: if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr); ipver = "IPv4"; } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; addr = &(ipv6->sin6_addr); ipver = "IPv6"; } // convert the IP to a string and print it: inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); printf(" %s: %s\n", ipver, ipstr); } sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) { printf("Connection error\n"); perror(argv[0]); return errno; } else printf("Connection successful\n"); close(sockfd); freeaddrinfo(res); // free the linked list return 0; }
The nice thing about it is that you can use errno to return the error. This will give you a number of different errors to determine why it cannot connect so you can see if there is just some kind of configuration issue or if if there is just no connection.
- 02-09-2012 #7Just Joined!
- Join Date
- Feb 2012
- Posts
- 2
check the internet connection
hy,
if you want to check the connection you must send and receive something. it can be done via socket for example. I suggest you to create a socket. as it was mentioned above if you have success tcp socket you can be sure you have connection to that server. but if you use ROW_SOCKET and implement an ICMP, than you can use echo (ping does the same). As it is guessed out because of similar problems as it is. I think it is a best way.
T



