Thread: Retrieving current PC IP Address? Please help...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Retrieving current PC IP Address? Please help...

    Hello. I am creating a basic two-person instant messenger-like application, and I need some help. If I use the "gethostbyname()" function, how would I get the user's IP address? I know there is a member of the "hostent" structure called "h_addr_list". How would I use that member to copy the IP address into a string?

    Code I have myself:

    Code:
    char host[201];         // Hostname string
    gethostname(host, 200);        // Get current hostname
    
    hostent *h = gethostbyname(host);        // Function to get host information
    
    // What to do with the "hostent *h" object?
    // ...............
    Thanks in advance.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you want the external IP address, I don't think you can get it without asking another PC over the internet what it is.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read beej's guide (link in the links thread)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Any given computer will have one or more associated ip addresses. When you call gethostbyname(), you validate the pointer, then access it's char * h_addr_list[], cycling though till a null pointer is reached. During each of those iterations, you will cast that index's char * to an unsigned long *, dereference that pointer to get each available address (which is in network byte order).

    OK, so I don't explain very clearly so I will resort to an example.

    Code:
    /* get_ips() returns the number of addresses copied to addr_buff.
    Optionally, you can pass a pointer the count as the last parameter. */
    
    int get_ips(const char * hostname, unsigned long addr_buff[], int max, int * count = 0)
    {
     int index = 0;
    
     if(count == 0) count = &index;
    
     hostent * host = gethostbyname(hostname);
    
      if(!host && max != 0)
     {
        if((addr_buff[index] = inet_addr(hostname)) != INADDR_NONE)
         ++index;
     }
      else
     {
        for( ; host->h_addr_list[ index ] && index < max; ++index )
       {
         addr_buff[index] = ( *(unsigned long*)host->h_addr_list[ index ] );
       }
     }
    
     return *count = index;
    }
    
    
    
    
    int local_host(unsigned long addr_buff[], int max, int * count = 0)
    {
     const int buff_size = 10240;
     char local[buff_size];
     gethostname(local, buff_size);
     return get_ips(local, addr_buff, max, count);
    }

    Of course, any variation off that basic theme will do.

    Here's a basic program:


    Code:
    int main()
    {
     const int max = 246; // *just in case* :p
     unsigned long addresses[max];
     
     int total = local_host(addresses, max);
    
     cout << "There are " << total << " addresses available." << endl
     << "The are:" << endl;
    
     for(int index = 0; index < total; ++index)
      cout << inet_ntoa(addresses[index]) << endl;
    
     return cin.get();
    }

    Happy coding.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, there's:

    1.gethostname() followed by gethostbyname()
    2.connect() followed by getsockname()

    3.connect() to www.whatsmyipaddress.com or something similar, get the html file and use some algorithm to filter out the useless HTML tags etc. and "your ip is" and figure out what it says your ip address is

    1) is hard to use; it means you need to take a lot of steps to figure out which ip you want; if you're behind a router it might not work anyways.
    2) means you need to make a connection first; www.google.ca should suffice, but if you're behind a router it won't work anyways.
    3) is really slow and unreliable, as the site/layout may change at any time and might go down, etc. and also is limited by the speed of the server.

    I ended up doing 2+3; I read a text file list of all the reliable sites I know and try each one until one accepts a connection; then I added a little text message at the bottom that says "if you're behind a router, visit www.whatsmyipaddress.com for your real address."
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  3. How to get IP address in C?
    By perlguy in forum C Programming
    Replies: 2
    Last Post: 06-08-2002, 08:42 PM
  4. IP address
    By Jackie in forum C Programming
    Replies: 9
    Last Post: 03-14-2002, 07:06 AM
  5. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM