Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    5

    Post 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.

  2. #2
    Linux Guru Rubberman's Avatar
    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!

  3. #3
    Linux 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
    done
    In a world without walls and fences, who needs Windows and Gates?

  4. #4
    Just 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.

  5. #5
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Quote Originally Posted by umar420e View Post
    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.
    Don't think so, but only other way I can think of doing it is to open a connection to know reliable server and hold the connection open (sort of like a heartbeat). If the internet connect drops the connection will/should close.
    In a world without walls and fences, who needs Windows and Gates?

  6. #6
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162
    Quote Originally Posted by blinky View Post
    Don't think so, but only other way I can think of doing it is to open a connection to know reliable server and hold the connection open (sort of like a heartbeat). If the internet connect drops the connection will/should close.
    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...

    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;
    }
    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.

    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.

  7. #7
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...