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

Threaded View

  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.

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