共用方式為


Tired of creating sockets for each protocol

Tired of creating sockets for each protocol, to make your application listen on all addresses for a given port ? .

There is no more pain. Here is a cool new feature supported in Vista and Windows Server Longhorn. By just setting a socketoption, you will be able to use the same socket to accept connections from both ipv4 and ipv6 clients.

The following sample code shows how to configure the server socket.

  static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            sock.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0);
            sock.Bind(new IPEndPoint(IPAddress.IPv6Any, 8000));
            sock.Listen(4);
            Socket client = sock.Accept();
            Console.WriteLine("Client has connected successfully with the server");
        }

Create an IPv6 socket. Set the socket option IPV6_V6Only to false (by default it is true), to enable the socket to accept connections from ipv4 addresses too. Bind and Listen as usual. Now both ipv4 and ipv6 clients can connect to it
More info can be found in https://www.ietf.org/rfc/rfc3493.txt.

This is because Vista has a completely redesigned TCP/IP stack.

Whereas Windows XP and Win2k3 have a dual stack architecture, i.e., the Tcp, Udp layer is separate for both ipv4 and ipv6 protocols, Vista supports the dual IP layer architecture. That is, Only a single Tcp, Udp layer is present for both the ipv4 and ipv6 protocols.

Other features of the next Generation TCP/IP stack supported in Vista can be found at https://www.microsoft.com/technet/community/columns/cableguy/cg0905.mspx

Comments

  • Anonymous
    June 13, 2007
    I was recently asked about sample code for the System.Net.Security.SslStream using the asynchronous APIs.