Computer has 2 ip address
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Computer has 2 ip address

  1. #1
    Join Date
    Mar 2011
    Posts
    48

    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,797

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,329

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center