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

    Find LAN IP amongst Virtual IPs

    I'm writing a .Net 2.0 application in C#. Given a computer name, I want to look up the IP address for that machine. If there are IPv6 and IPv4 addresses, I want to return the IPv4 address. However I recently received a bug report that when there are Virtual Machine instances installed, the IP address that comes back is the VM's IP and not the LAN IP of the computer.

    Is there any way to distinguish between a VM IP and a LAN IP if both are IPv4 format?

    Code:
    public string GetIPAddress(string PCName)
    {
         string strRet = String.Empty;
         foreach (IPAddress adr in Dns.GetHostEntry(PCName).AddressList)
         {
              // Sets any IPv4 addresses to the return string
              if (adr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
              {
                   strRet = adr.ToString();
                   break;//Fine if no VM installed, but otherwise gets the wrong IP
              }
         }
         return strRet;
    }
    Last edited by nice_guy_mel; March 31st, 2010 at 10:40 AM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Find LAN IP amongst Virtual IPs

    I don't know C#, but your description doesn't sound correct. VM or not shouldn't matter, as the VM running should have a different computer name then the host machine. Otherwise, how would you refer to it by name (ah-la DNS lookup)?

    Viggy

  3. #3
    Join Date
    Mar 2005
    Posts
    99

    Re: Find LAN IP amongst Virtual IPs

    I'm not very familiar with VM and how it works, but what I've seen is that when we install a VM instance on one of our computers, then do a ipconfig command we see listed:
    Local Area Connection: X.X.X.X the LAN IP of the computer
    and
    VMWare Network Adapter: 192.168.X.X the VM's IP

    Our code seems to work fine if I'm on Computer A and I'm doing an IP lookup for Computer B (Computer B is the one with the VM installed). The problem only occurs when I'm on Computer B and doing an IP lookup for Computer B. Since both are IPv4 it's difficult to tell which is the LAN IP. I might be able to get away with ignoring any IPs that start with 192.168, since all examples I've seen have started with these numbers. However I thought there might be a more appropriate solution. Do you think my question would do better in the .Net forum instead of the Networking one?
    Last edited by nice_guy_mel; April 1st, 2010 at 05:45 AM.

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Find LAN IP amongst Virtual IPs

    Possibly...

    If your host machine is on a different network then your virtual machine, then the host is doing some kind of NAT (acting as a router). Again, I find it hard to believe that if you're doing a lookup by name, you're getting the virtual machine's address. Actually, what you're probably getting is the address of the virtual adapter. But, if you're communicating with yourself, should this matter?

    Viggy

  5. #5
    Join Date
    Mar 2005
    Posts
    99

    Re: Find LAN IP amongst Virtual IPs

    Sorry, I thought I made that clear. The GetHostEntry is returning both the LAN IP of the current computer and any VM adapter IPs. Basically the same IPs that a 'ipconfig' call would return. It doesn't have anything to do with the VM being on a different network.

    This is not a showstopper for our customer, because we can tell them to input the LAN IP of the computer instead of the computer name. However most of our customers are more familiar with the computer names rather than the IPs.

    It turns out I can't simply ignore any 192.168 addresses, because the customer's LAN IP is actually a 192 address. I'm stumped as to how to tell the difference between the VM adapter IPs and the LAN one.

  6. #6
    Join Date
    Mar 2005
    Posts
    99

    Re: Find LAN IP amongst Virtual IPs

    Still curious about this issue.

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Find LAN IP amongst Virtual IPs

    How about the routing tables?

  8. #8
    Join Date
    Mar 2005
    Posts
    99

    Re: Find LAN IP amongst Virtual IPs

    Quote Originally Posted by hoxsiew View Post
    How about the routing tables?
    Do you have a code sample of your idea?

  9. #9
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Find LAN IP amongst Virtual IPs

    The IPHelper API would be my first try. There may even be a better way with other IPHelper functions.

    Here's a pretty good rundown on most of the features IPHelper has to offer in a simple dialog based project:

    http://www.codeproject.com/KB/IP/IPHelper.aspx

  10. #10
    Join Date
    Aug 2010
    Posts
    1

    Re: Find LAN IP amongst Virtual IPs

    Hi,
    there is a link given below where you can get your solution for a question about lan ip and virtual ip. Thank you.

    http://www.canbal.com/index.php?/Usi...l-machine.html



    ---------------------------------------------------------------------------------------------------------------------

    Want to get-on Google's first page and loads of traffic to your website? Hire a SEO Specialist from Ocean Groups seo specialist

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