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()
            IPHostEntry hostent = Dns.Resolve("localhost");
            foreach(IPAddress addr in hostent.AddressList)
                Console.WriteLine(String.Format("{0} [{1}]",
        catch(Exception e)
            Console.WriteLine(String.Format("Caught: {0}", e.GetType().ToString()));

Output from above sample code:
::1 [InterNetworkV6] [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

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.

Comments (2)

  1. Neil Cowburn says:

    Great stuff! I really like the idea that IPv6 addresses are resolved first. I think this is the first example I’ve seen of something having a preference for IPv6 as opposed to IPv4. Nice piece of future-proofing and it will assist in the mindset change over to the new addressing schema.

Skip to main content