Click to See Complete Forum and Search --> : Find LAN IP amongst Virtual IPs


nice_guy_mel
March 31st, 2010, 10:35 AM
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?


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;
}

MrViggy
March 31st, 2010, 12:20 PM
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

nice_guy_mel
April 1st, 2010, 05:42 AM
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?

MrViggy
April 1st, 2010, 10:05 AM
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

nice_guy_mel
April 1st, 2010, 10:33 AM
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.

nice_guy_mel
August 6th, 2010, 05:46 AM
Still curious about this issue.

hoxsiew
August 6th, 2010, 07:55 AM
How about the routing tables?

nice_guy_mel
August 6th, 2010, 10:49 AM
How about the routing tables?

Do you have a code sample of your idea?

hoxsiew
August 7th, 2010, 10:17 AM
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

stevephillips79
August 26th, 2010, 04:51 AM
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?/Using-C-Sharp/c-virtual-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 (http://oceangroups.org/)