Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IPv6 Client Server Network Applications in C# .NET

0.00/5 (No votes)
6 Aug 2003 3  
IPv6 programming in the C# .NET 1.1 environment

IPv6 Client Server Network Applications in C# .NET

Before reading any further, it is worth mentioning that this requires the .NET framework Version 1.1. This article shows a simple TCP based application in IPv6 as well as how to configure your machine to enable IPv6 .NET development.

Introduction to IP

Many people have heard of IPv6 and its apparent necessity to replace the current IPv4 system of address allocation.

The next two sections give a brief introduction to IPv4 and IPv6, you may want to skip these two sections and go straight to the section IPv6 Client Server Applications in C# .NET 1.1.

A Brief Introduction to IPv4

IPv4 is the current system in use that assigns IP addresses to a system, every machine connected directly to the Internet has a unique IP(v4) address. An IP address is displayed in human readable decimal form as xxx.xxx.xxx.xxx, where each section can store decimal 255 or Hex FF (8 bits). This means an IPv4 address is 32bits long, allowing 4294967296 (232) unique values. The current population of the world as estimated by the United Nations, stands at approximately 6 billion, with the global population increasing by approximately 79 million a year(PRB01). Although every person in the world does not currently use the Internet, this number is much higher than the amount of addressable IPv4 addresses. In fact, unallocated IPv4 addresses are in short supply, according to the IETF (Internet Engineering Task Force) IPv4 addresses will be exhausted by 2008 (±3 years).

Fortunately, methods for getting around the IPv4 problem have been designed. NAT (Network Address Translation) allows N machines on a private network to share one public IP address. In such a setup, the private network machine would request data from the Internet; the NAT would translate the user’s local IP address into a public IP address and add information regarding the source of the request. When the reply comes back from the Internet the NAT machine sends this information back to the private machine. This means machines on the private side of the NAT cannot truly communicate bi-directionally with machines on the Internet as the private machines IP address is not valid on the public network. This severely limits what that machine can do, for example, that machine would not be able to fully participate on a distributed network such as GnuTella, as it would not be able to act as the server part of the system. Setting up file transfer on applications such as MSN Messenger also becomes a problem as direct connections between machines cannot be established, especially if both machines trying to communicate are behind NAT gateways.

A Brief Introduction to IPv6

The problems mentioned in the introduction to IPv4 didn’t go unnoticed within the Internet community. In 1992, the IETF began efforts to find a next generation protocol for the Internet. Two years of proposals later, SIPP (Simple Internet Protocol Plus) was adopted and in 1995, it was named Internet Protocol Version 6 (IPv6). An IPv6 address is displayed in human readable hexadecimal form as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx, where each divided bit can have a maximum decimal store of 65535 or Hex FFFF (16 bits). This means an IPv6 address is 128bits long, allowing 3.4x1038 (2128) unique values. “If the address space of IPv4 is compared to 1 millimetre, the address space of IPv6 would be 80 times the diameter of the galactic system”(WIDE01). There are many additional features of IPv6, including support for IPSec, a ‘Cluster Address’ field which identifies topological regions and source routing which controls routing in a more precise way (IPv4 currently requires significant routing overhead).

IPv6 Client and Server Applications in C# .NET 1.1

There are a few things you need to setup before being able to write and test IPv6 applications on your machine. Based on your operating system, you will need to use different methods to install IPv6. Windows 2000 users should look at http://research.microsoft.com/msripv6/, while Windows XP users should read http://www.microsoft.com/windowsxp/pro/techinfo/administration/ipv6/default.asp for more information. As the .NET framework will eventually run on all operating systems, other operating system users should go to http://www.hs247.com/ for more information. The rest of this document will assume you are running Windows XP SP1.

Preparing Your System to Run IPv6 Code

I have installed the Microsoft Loopback device on my machine because I do not have a permanent network connection, however you can use your local system's loopback device for this example (::1, which in IPv4 is equivalent to 127.0.0.1). To install the Microsoft loopback device, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcepb40/htm/pbtskinstallingmicrosoftloopbackadapter.asp.

Start a command prompt window and type ipv6 install. After a short period of time, your machine will be IPv6 enabled. You can check this by typing ipconfig at the command prompt. You should see an output similar to this:

Windows IP Configuration

Ethernet adapter Microsoft Loopback:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 192.168.1.10
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        IP Address. . . . . . . . . . . . : fe80::4cff:fe4f:4f50%4
        Default Gateway . . . . . . . . . :

Tunnel adapter Teredo Tunneling Pseudo-Interface:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : fe80::54ff:fe55:4e01%6
        Default Gateway . . . . . . . . . :

Tunnel adapter Automatic Tunneling Pseudo-Interface:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : fe80::5efe:192.168.1.10%2
        Default Gateway . . . . . . . . . :

The address fe80::4cff:fe4f:4f50 has been assigned to the Microsoft Loopback device. You should be able to use the ping6 command to ping this address (changing it to the address assigned to your machine).

C:\Documents and Settings\Gary>ping6 fe80::4cff:fe4f:4f50

Pinging fe80::4cff:fe4f:4f50
from fe80::4cff:fe4f:4f50%4 with 32 bytes of data:

Reply from fe80::4cff:fe4f:4f50%4: bytes=32 time<1ms
Reply from fe80::4cff:fe4f:4f50%4: bytes=32 time<1ms

This is the IPv6 address that will be used in the code samples, you can also change this address to your systems loopback device ::1.

Preparing .NET SDK Framework 1.1 for IPv6 Applications

Open the machine.config file that is located in the .NET Framework CONFIG directory (on Windows XP it is located at C:\Windows\Microsoft.NET\Framework\v1.1.XXXX\CONFIG). Change the XML tag:

<!-- <ipv6 enabled="false"/> --> 

to:

<ipv6 enabled="true"/> 

This allows the framework to parse and resolve IPv6 addresses. We will do a test, in code, to ensure this is working correctly later.

Server Implementation

There are many similarities between building an IPv4 TCP Server and an IPv6 TCP Server. This is great news for developers looking to upgrade current IPv4 applications to IPv6, no matter which language is being used.

First, it is necessary to import the following namespaces:

using System.Net.Sockets;
using System.Net;
using System;

Second, check to ensure the system has support for IPv6, this can be done by using the Socket.SupportsIPv6 boolean value.

const int PORT=1979;

if(!Socket.SupportsIPv6) {
    Console.Error.WriteLine("Your system does not support IPv6\r\n" +
        "Check you have IPv6 enabled and have changed machine.config");
    return;
}

We then create an initial listening socket.

Socket listener = new Socket(
    AddressFamily.InterNetworkV6,
    SocketType.Stream,
    ProtocolType.Tcp);

The AddressFamily is set as InterNetworkV6 (rather than InterNetwork for IPv4).

listener.Bind(new IPEndPoint(IPAddress.IPv6Any, PORT));
listener.Listen(0);

We then bind to the IP Address IPv6Any – this means the application will listen on all NIC addresses on the system. We then call listener.Listen(0) on the socket. This will block until a connection is made to the socket on port 1979. You can check that you are correctly listening to the port by typing netstat –a at a command prompt. You should see a line similar to this in the output:

TCP    yourmachinename:1979   [::]:0          LISTENING       0

We then process anything that comes onto the port and display it to the screen, finishing with a closure of all the sockets and exiting the application:

Socket socket = listener.Accept();
listener.Close();

byte[] b = new byte[11];
int len;
while((len = socket.Receive(b)) != 0) {
    System.Console.WriteLine("RX: " +
        System.Text.ASCIIEncoding.ASCII.GetString(b, 0, len));
    b = new byte[11];
}
socket.Close();

The socket accepts 11 bytes of data and then closes all the sockets when the client disconnects.

Client Application

Again, an IPv6 client application is very similar to an IPv4 application.

First, import the following namespaces:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

System.Threading is included for example purposes to introduce a delay in sending information to the server.

const int PORT = 1979;
const string IPv6_ADDR = "fe80::4cff:fe4f:4f50";

if(!Socket.SupportsIPv6) {
    Console.Error.WriteLine("Your system does not support IPv6\r\n" +
        "Check you have IPv6 enabled and have changed machine.config");
    return;
}

We set the IPv6_ADDR string to the IPv6 address that was returned by ipconfig, change this to the IPv6 address of your machine or ::1 for your systems loopback device.

IPAddress ipa = IPAddress.Parse(IPv6_ADDR);
IPEndPoint ipeh = new IPEndPoint(ipa, PORT);
Socket connection = new Socket(
    AddressFamily.InterNetworkV6,
    SocketType.Stream,
    ProtocolType.Tcp);

The Dns.Resolve method does not currently return the IPv6 address specified at IPv6_ADDR. The socket is created with the address family of InternetNetworkV6, exactly the same method as shown in the server code.

connection.Connect(ipeh);

byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes("hello world");
for(int x=0; x<10; x++) {
    Console.WriteLine("TX: " + System.Text.ASCIIEncoding.ASCII.GetString(b));
    connection.Send(b);
    
    Thread.Sleep(1000);
}

connection.Close();

The application then connects to port 1979 at the address IPv6_ADDR and sends the ASCII string ‘hello world’ 10 times every second. The socket is then closed. This terminates both the applications. Remember to run the server application code first before executing the client code.

Further Reading

There is plenty of information on the Internet regarding IPv6. A few very useful links are:

Microsoft’s IPv6 Portal http://www.microsoft.com/ipv6/
UK IPv6 Resource Centre http://www.cs-ipv6.lancs.ac.uk/ipv6/
6Bone.Net http://www.6bone.net/
IPv6 News and Links http://www.hs247.com/
WIDE Project http://www.v6.wide.ad.jp/
IPv6 Information http://www.ipv6.org/

Also try searching for ‘IPv6’ at http://www.google.com/.

References

History

  • 30/01/2003: First version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here