Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I am developing an application in C++ on redhat 9. The application needs to know whether the current machine running the application is on the network or not under ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2005
    Posts
    1

    How to detect whether the machine is on the network from application?


    Hi,
    I am developing an application in C++ on redhat 9. The application needs to know whether the current machine running the application is on the network or not under some situation. Currently, the application tries to ping the gateway to find out by calling "system("ping gateway_IP")".
    My question is: Is there anyway other than using "ping" method? Is there a general way to accomplish this?
    I am appreciated of any suggestion.

  2. #2
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    If you know an external URL or ip address you can use code like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    
    int main&#40;int argc, const  char **argv&#41;
    &#123;
    
         struct hostent *hp;
         char **p;
    
         hp=gethostbyname&#40;argv&#91;1&#93;&#41;;
         if &#40;hp == NULL&#41; &#123;
              &#40;void&#41; printf&#40;"host information for %s no found \n", argv&#91;1&#93;&#41;;
              exit &#40;3&#41;;
         &#125;
    
         for &#40;p = hp->h_addr_list; *p!=0;p++&#41;&#123;
             struct in_addr in;
             char **q;
    
             &#40;void&#41;memcpy&#40;&in.s_addr, *p, sizeof&#40;in.s_addr&#41;&#41;;
             &#40;void&#41;printf&#40;"%s\t%s",inet_ntoa&#40;in&#41;, hp->h_name&#41;;
             for &#40;q=hp->h_aliases;*q != 0; q++&#41;
                  &#40;void&#41; printf&#40;"%s", *q&#41;;
            &#40;void&#41;putchar&#40;'\n'&#41;;
         &#125;
         return 0;
    &#125;
    gethostbyname will return an error if the DNS cannot be found - ie., usually because internet is down. This works if you have an external DNS.
    If you are your own internet provider -- your company has it's own DNS - this won't work.

  3. #3
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    Oops. thought you wanted internet try:
    Code:
    /*
     * gethwaddr.c
     *
     * Demonstrates retrieving hardware address of adapter using ioctl&#40;&#41;
     *
    
     *
     */
    #include <stdio.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <net/if.h>
    /* linux only */
    
    int main&#40; int argc, char *argv&#91;&#93; &#41;
    &#123;
            int s;
            struct ifreq buffer;
    
            s = socket&#40;PF_INET, SOCK_DGRAM, 0&#41;;
            memset&#40;&buffer, 0x00, sizeof&#40;buffer&#41;&#41;;
            strcpy&#40;buffer.ifr_name, "eth0"&#41;;
            ioctl&#40;s, SIOCGIFHWADDR, &buffer&#41;;
            close&#40;s&#41;;
            for&#40; s = 0; s < 6; s++ &#41;
            &#123;
                    printf&#40;"%.2X ", &#40;unsigned char&#41;buffer.ifr_hwaddr.sa_data&#91;s&#93;&#41;;
            &#125;
            printf&#40;"\n"&#41;;
            return 0;
    &#125;
    s will be minus one and ioctl will return an error as well -- if there is no network connection functioning. errno will be set to EHOSTDOWN when the network is unhappy.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401

    Simple connect?

    If your machines run some service, you can use a simple connect... if connect fails, host is down (probably)...

    However you can implement what the program ping do, instead executing it with a system...

    You can also implements special "half connect", the same used by nmap port scanner...
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

Posting Permissions

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