Freigeben über


Using a single socket to accept IPv6 of IPv4 connections

Generally I don't like making a post that only includes links to other posts, but I'm making an exception for this case.  In this (https://blogs.msdn.com/malarch/archive/2005/11/18/494769.aspx) article Malar explains how to create a single socket that can listen for IPv6 and IPv4 connections. This ability is due to the new TCP/IP stack in Vista. Malar uses the following code to demonstrate creating such a 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");
}

The socket option being set above is pound (#) defined in Winsock as IPv6Only; however, since this is a Vista feature, we don't have the name encapsulated in the SocketOptionName enumeration yet. I should note, the above is not supported in Whidbey, but we are working to enable this scenario in a future release.