IPv6 and the NetCF v2 beta

One of the new features of the .NET Compact Framework v2 beta is support for IPv6. Unlike the full .NET Framework, you do not need to explicitly enable IPv6 in NetCF -- if your device has support for IPv6 installed, it will be available to NetCF applications.

On devices supporting both IPv6 and IPv4 addressing, when resolving a host (localhost, in this example) the IPv6 addresses are always listed first in the resulting IPHostEntry.AddressList field.  The code below resolves the local machine (via "localhost") and displays the addresses as they appear in the IPHostEntry.AddressList field:

using System;
using System.Net;

public class TestClass
{
public static void Main()
{
try
{
IPHostEntry hostent = Dns.Resolve("localhost");
foreach(IPAddress addr in hostent.AddressList)
{
Console.WriteLine(String.Format("{0} [{1}]",
addr.ToString(),
addr.AddressFamily.ToString()));
}
}
catch(Exception e)
{
Console.WriteLine(String.Format("Caught: {0}", e.GetType().ToString()));
Console.WriteLine(e.Message);
}
}
}

Output from above sample code:
::1 [InterNetworkV6]
127.0.0.1 [InterNetwork]

Of course, resolving localhost is not very interesting -- replacing "localhost" with my machine name yields a better example (actual addresses have been removed):
<<IPv6 over IPv4>> [InterNetworkV6]
<<IPv6 #1>> [InterNetworkV6]
<<IPv6 #2>> [InterNetworkV6]
<<IPv4 #1>> [InterNetwork]
<<IPv4 #2> [InterNetwork]

Note: The above system has two network adapters.

This behavior effects how the HTTP client attempts to communicate with a remote server -- if the remote machine supports IPv6, the v6 address will be tried first. If you wish to force the HTTP client to use an IPv4 address, you will need to resolve the host yourself and then replace the host name with it's corresponding v4 address.

Until next time.
-- DK

Disclaimers:
This posting is provided "AS IS" with no warranties, and confers no rights.
The information contained within this post is in relation to beta software. Any and all details are subject to change.