|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IPv6 Client Server Network Applications in C# .NETBefore 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 IPMany 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 IPv4IPv4 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 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 IPv6The 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 IPv6 Client and Server Applications in C# .NET 1.1There 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 codeI 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 ( Start a command prompt window and type 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 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 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 ensue this is working correctly later.
Server ImplementationThere 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 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 listener.Bind(new IPEndPoint(IPAddress.IPv6Any, PORT));
listener.Listen(0);
We then bind to the IP Address 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 ApplicationAgain, 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;
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 IPAddress ipa = IPAddress.Parse(IPv6_ADDR);
IPEndPoint ipeh = new IPEndPoint(ipa, PORT);
Socket connection = new Socket(
AddressFamily.InterNetworkV6,
SocketType.Stream,
ProtocolType.Tcp);
The 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 Further ReadingThere is plenty of information on the Internet regarding IPv6. A few very useful links are:
Also try searching for ‘IPv6’ at http://www.google.com/. References(WIDE01) WIDE Project, Concluded Working Groups, IPv6 (PRB01) World Population Data Set History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||