Reputation: 11140
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
Upvotes: 7
Views: 14385
Reputation: 41718
To listen on both the IPv4 and IPv6 stacks, use this code:
var listener = new TcpListener(IPAddress.IPv6Any, 2222);
listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
IPv6Any
tells Windows to listen on the IPv6 stack. Setting the socket option to false tells Windows to not limit itself to the IPv6 stack, but rather to also listen on the IPv4 stack. The default is to only listen on the stack explicitly specified.
Upvotes: 7
Reputation: 13864
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.
Upvotes: 12