C# – What’s the difference between IPAddress.Any and IPAddress.IPv6Any fields

cnetwork-programmingnetworking

I tried the following code to make my code work in dual stack mode. Unfortunately, it's not opening port in the dual stack mode.

var listener = new TcpListener(IPAddress.Any, 2222);
listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
listener.Start();

Later I realized that changing to var listener = new TcpListener(IPAddress.IPv6Any, 2222); would make it work for me. What exactly is the difference between

IPAddress.Any and IPAddress.IPv6Any fields?

The documentation on MSDN is a bit vague

Best Answer

IPAddress.Any is for all IPv4 interfaces, IPAddress.IPv6Any is for all IPv6 interfaces.

IPAddress.Any is 0.0.0.0, IPAddress.IPv6Any is ::

If you just use IPv6Any without using SocketOptionName.IPv6Only then you can accept both IPv4 and IPv6 connections on the same socket.