CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    60

    Computer has 2 ip address

    Hello,

    Thank you for taking the time to help me out. I'm using the following routine to find the ipaddress of the computer the application is being run on:

    Code:
    do
       {
          char ac[80];
          if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
             log("error when getting host name %d\n", WSAGetLastError());
             return;
          }
          log("Host name is %s\n", ac);
    
          struct hostent *phe = gethostbyname(ac);
          if (phe == 0) {
             log("Yow! Bad host lookup.\n");
             return;
          }
          string ipaddress;
          for (int i = 0; phe->h_addr_list[i] != 0; ++i) 
          {
             struct in_addr addr;
             memcpy_s(&addr, sizeof(addr), phe->h_addr_list[i], sizeof(struct in_addr));
             log("IP Address is %s\n", inet_ntoa(addr));
             ipaddress = inet_ntoa(addr);
    
             if(ipaddress == "192.168.1.43")
             {
                flag = 1; 
                ....something happens          
             }
             else if(ipaddress == "192.168.1.45")
             {
                flag = 1;
                ....something happens 
             }
             else if(ipaddress == "192.168.1.47")
             {
                flag = 1;
                ....something happens 
             }
             else
                log("error IP address is not a valid address\n");
          }
    
       }while(flag == 0);
    This code is giving me 2 ip addresses. First it's giving me 192.168.1.45, then it gives me 192.168.1.47. The first one is the right one, but because the way the code is written the second one overwrites the first one. I'm guessing this is happening because at one point there was a usb network adapter hooked up to the machine and it's somehow remembering that ipaddress. Using the ipconfig command in the console reveals the correct ipaddress but a little farther down the console reads something like "media device disconnected." So I'm guessing it's remembering the ip given by that usb adapter. Now my question is will the ipaddress the computer is using always be the first one I get using this routine? Because I could easily just insert a break statement. Or is there another way to discern which ipaddress the computer is currently using? Thanks for the help.
    Last edited by Gregorina; May 8th, 2014 at 04:59 PM.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Computer has 2 ip address

    >> The first one is the right one, ...

    >> for (int i = 0; phe->h_addr_list[i] != 0; ++i)
    Why not just remove the loop and use h_addr_list[0] only?

    gg

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Computer has 2 ip address

    I'm using the following routine to find the ipaddress of the computer the application is being run on Now my question is will the ipaddress the computer is using always be the first one I get using this routine?
    It is perfectly correct for a computer to have more than one valid ip address. I have a computer configured with 4 different ip addresses for access to different sub-nets. Depending upon your setup and future network changes, relying upon the first value to be the one ip address that 'the computer is using' could lead to later problems.

    Depending upon how the found ip address is going to be used, you may need to deal with having more than one valid ip address.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured