1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Linux - How to get my IP in C/C++

Discussion in 'Software Development' started by rockballad, Nov 8, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. rockballad

    rockballad Thread Starter

    Joined:
    May 4, 2007
    Messages:
    78
    Hi,

    I'm writing a small program to get my computer IP on LAN (e.g. 192.168.x.x, it's not 127.0.x.x). In Windows, it's simple, but I can't do it in Linux (code is quite similar). If you know about it, could you show me please?

    Thanks in advance.
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I can't do it in C or C++ but a shell script would be super easy. I can do just about anything in Linux with a shell script.
    Code:
    ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |
    cut -d: -f2 | awk '{ print $1}'
     
  3. rockballad

    rockballad Thread Starter

    Joined:
    May 4, 2007
    Messages:
    78
    Thank you! It should work well! I just set some first steps on Linux, so your help is really appreciated! I'll search on how to grab the output of a commandline or something like that. But, I think indeed that must be a way to do in C/C++.

    Cheers,
     
  4. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi Squashman,

    Your example doesn't work correctly as it needs to consider the context of whether a user's connection is dialup or not:

    if a user is dialup then the following should work:
    ifconfig ppp0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

    otherwise,

    your scripting produces the following on a dialup system with an ethernet card (unplugged):
    eth0:avah: <n.n.n.n>
    ppp0: <the correct ip address>

    -- Tom
     
  5. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi rockballad,

    One way to feed a value in your computing environment is to assign the found ip address to an environment variable, and then read the environment variable into the c/c++ program.

    -- Tom
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Yes, but he asked for is LAN IP.

     
  7. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Ooops, missed that!

    -- Tom
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Don't Sweat it. You are still the Linux Guru around here since Code Jockey hasn't been around much.
     
  9. windows user

    windows user

    Joined:
    Sep 3, 2007
    Messages:
    60
  10. rockballad

    rockballad Thread Starter

    Joined:
    May 4, 2007
    Messages:
    78
    Thank you all.

    Yes, it's expected to work. But it's not :( The func gethostbyname("localhost") returns "127.0.0.1" only. And, gethostbyname("mycomputername") returns "127.0.1.1".

    From the article, I though that why I don't try to get the *real* host from the IP (that I expect to get).

    The command "ifconfig" in Terminal let me know that the IP on LAN is "192.168.1.5". So by using
    Code:
    inet_aton("192.168.1.5", &addr);
    h = gethostbyaddr(&addr, sizeof addr, AF_INET);
    
    I know the *real* host name is "mycomputername.local"! That's it. The difference is the tail ".local". Could any one tell me more about this tail and others? Maybe this's the difference between Linux and Windows, isn't it?

    Thanks again! (y)
     
  11. flowerbed

    flowerbed

    Joined:
    Nov 14, 2007
    Messages:
    1
    My Microsoft ME 2000 is not locking onto IP address. I have no Internet access. Please help.
     
  12. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi flowerbed,

    Welcome to TSG!

    You need to read the forum rules.

    Then use the New Thread button at the top of the Forum webpage to open up your own new thread.

    -- Tom
     
  13. windows user

    windows user

    Joined:
    Sep 3, 2007
    Messages:
    60
    Post your code. Here's what I did and it worked. I copied and pasted from Beej's tutorial:
    Code:
    #include <sys/unistd.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <stdio.h>
    
    int main()
    {
     char hostname[128];
     int i;
     struct hostent *he;
     struct in_addr **addr_list;
     struct in_addr addr;
    
     gethostname(hostname, sizeof hostname);
     printf("My hostname: %s\n", hostname);
    
     he = gethostbyname(hostname);
    
     if (he == NULL) { // do some error checking
       herror("gethostbyname"); // herror(), NOT perror()
       return 1;
     }
    
     // print information about this host:
     printf("Official name is: %s\n", he->h_name);
     printf("IP address: %s\n", inet_ntoa(*(struct in_addr*)he->h_addr));
     printf("All addresses: ");
     addr_list = (struct in_addr **)he->h_addr_list;
    
     for(i = 0; addr_list[i] != NULL; i++) {
       printf("%s ", inet_ntoa(*addr_list[i]));
     }
    
     printf("\n");
    
     return 0;
    }
    
    Output:

    My hostname: kara
    Official name is: kara
    IP address: <something that isn't 127.0.0.1>
    All addresses: <something that isn't 127.0.0.1>
     
  14. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    Yep, that's exactly what I was going to suggest. Plus, this should work across platforms as well.

    Peace...
     
  15. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    It relates to how your local hosts file is setup along with the DNS configuration. In your local hosts file, you will have an entry for "localhost" and possibly for your actual local host name. This is why gethostname() is called first, so you can get your actual host name. Then you call gethostbyname() to get the IP address.

    So, I imagine on your system, gethostname() returns "mycomputername.local". You then pass this to gethostbyname() to get your IP address, as described in the tutorial linked to above.

    Attached is a screenshot of the hosts file for my Gentoo Linux system running in VirtualBox on Windows XP, as an example.

    Peace...
     

    Attached Files:

  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/649627

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice