how to get local ip adress

This is a discussion on how to get local ip adress within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, How can I get local IP adress within a C program? I tried this with gethostname function but it ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    22

    how to get local ip adress

    Hi,
    How can I get local IP adress within a C program?

    I tried this with gethostname function but it can not be helped since i dnt know the host name of the machine.

    Thanks

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by wwwnuwan View Post
    Hi,
    How can I get local IP adress within a C program?

    I tried this with gethostname function but it can not be helped since i dnt know the host name of the machine.

    Thanks
    read about gethostname
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    22
    i tried with gethostname.
    but it returns the hostname like "myLaptop".
    I just need the ip like 192.168.10.10
    my prev code was like as follows
    Code:
        memset( hostname , 0 , MAXHOSTNAMELEN );
        gethostname( hostname,MAXHOSTNAMELEN );
        printf("MAXHOSTNAMELEN = %i \n " , MAXHOSTNAMELEN);

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Passing your hostname to gethostbyname() should work. Depending on how your system is set up though, this may just return "127.0.0.1".

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by bithub View Post
    Passing your hostname to gethostbyname() should work. Depending on how your system is set up though, this may just return "127.0.0.1".
    you get a list of IPs - one of it could be 127.0.0.1, you should iterate through the list to find all local IPs
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    22
    could you please give a some code example for this?

    now my programe is as follows.
    Code:
        char *hostname = malloc(MAXHOSTNAMELEN );
        memset( hostname , 0 , MAXHOSTNAMELEN );
        gethostname( hostname,MAXHOSTNAMELEN );
        struct hostent hostn = mallock(sizeof(struct hostent));
        gethostbyname(hostname);
    here then how can i get the ip and print it onto the console?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by wwwnuwan View Post
    could you please give a some code example for this?

    now my programe is as follows.
    Code:
        char *hostname = malloc(MAXHOSTNAMELEN );
        memset( hostname , 0 , MAXHOSTNAMELEN );
        gethostname( hostname,MAXHOSTNAMELEN );
        struct hostent hostn = mallock(sizeof(struct hostent));
        gethostbyname(hostname);
    here then how can i get the ip and print it onto the console?
    There is a lot of samples in the net...

    Winsock Programmer's FAQ: Get the Local IP Address(es) - for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    22
    Hi,
    Finaly i found the fellowing programs to obtain the local ip .
    but it returns 127.0.1.1
    please advice what goes wrong with this?

    Code:
    struct hostent *he;
    struct in_addr a;
    
      int main (int argc)
    {
        char *hostname = malloc(MAXHOSTNAMELEN );
        memset( hostname , 0 , MAXHOSTNAMELEN );
        gethostname( hostname,MAXHOSTNAMELEN );
        printf("hostname = %s \n " , hostname) ;
      he = gethostbyname (hostname);
      if (he)
      {
        printf("name: %s\n", he->h_name);
        while (*he->h_aliases)
          printf("alias: %s\n", *he->h_aliases++);
        while (*he->h_addr_list)
        {
          bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
          printf("address: %s\n", inet_ntoa(a));
        }
      }
      else
      printf("error \n");
        //herror(argv[0]);
      return 0;
    }
    Thanks.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    22
    I think this has no way to work out since my machine name (which I get by gethostname() method) is not in a DNS.
    so I think it returns 127.0.0.1

    any ideas about it?

    Thanks.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    22
    sory its 127.0.1.1

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Are you on windows or linux?

    On linux, gethostbyname() will just return whatever is in /etc/hosts. You can always add a line to the hosts file which has your correct IP address.

    Another option is to run "ipconfig" or "ifconfig" (depending on your OS), and parse out the result.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    What you want is the address of the actual interface. You can't really get at this information in a portable way.

    gethostbyname() is portable, but if it returns multiple answers you have no way of knowing which one is correct.

    Honestly, I'd bite the bullet and use a non-portable method to query the network interface and obtain its address directly.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Going with bithub's suggestion will be your best bet, especially if you have many interfaces, say a dual homed server with tied up vlans to them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 1
    Last Post: 09-11-2004, 09:52 AM
  3. getting local ip (not internet ip)
    By ipe in forum C Programming
    Replies: 12
    Last Post: 03-17-2003, 03:10 PM
  4. Replies: 2
    Last Post: 10-16-2002, 09:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21