views:

730

answers:

1

This is on a handheld connected to my computer via USB, using the Compact Framework:

When I call Dns.GetHostEntry("") or Dns.GetHostEntry(IPAddress.Loopback) to get the IPAddresses of the handheld, I get the IPAddresses of my host computer. But when I send a packet from the handheld to my computer, they come from a different address.

Is there a way to stop it from doing this? Would calling the DLLs directly via P/Invoke fix it?

The handheld isn't connected via WiFi or Bluetooth. The only connection is over USB.

+1  A: 

When you call Dns.GetHostEntry("") I'd think you'd get back a single entry in the IPHostEntry.AddressList of 127.0.0.1 (and, in fact, that's what a quick test shows). Dns.GetHostEntry(IPAddress.Loopback) returns two entries: 127.0.0.1 and ::1.

Neither is returning the IP address of the host PC, and neither should return the IP address of the device. If you want the IP address of the device, use something like this:

IPHostEntry hostent = Dns.GetHostEntry(Dns.GetHostName());

foreach (var addr in hostent.AddressList)
{
    Debug.WriteLine(addr.ToString());
}
ctacke
You're right. Dns.GetHostEntry(Dns.GetHostName()) returns the correct information, while Dns.GetHostEntry("") returns the wrong information. Even though the MSDN information says passing in an empty string will get you the IP address information about the local host. Also, you mention Dns.GetHostAddress which is not available in the compact framework.
Kris Peters
Sorry, I meant GetHostEntry. I've updated the answer.
ctacke