I've been working with sockets in .NET a bit lately and I'm wondering what the practical use of Socket.ExclusiveAddressUse
is. I've read the MSDN documentation so I know the basic idea (forcing a specific IP address/port combination to only allow one socket to bind to it) but I'm a little confused by what the property is actually used for.
The documentation says when ExclusiveAddressUse
is false:
If more than one socket attempts to use the
Bind(EndPoint)
method to bind to a particular port, then the one with the more specific IP address will handle the network traffic sent to that port.
How exactly is one IPEndPoint
(the only concrete subclass of EndPoint
that I can find) any more specific than another? How and why would you use this behavior in an application? Why would this behavior be the default in Windows versions later than XP but not before?
For instance if you have:
var endPoint = new IPEndPoint(IPAddress.IPv6Any, 800);
using (var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(endPoint);
socket.Listen(int.MaxValue);
socket.AcceptAsync(new SocketAsyncEventArgs().Completed += ...);
}
you'll start listening for all incoming connections from all IPv6 addresses on port 800. Now if you create another socket with an exact IP on the same port, the new socket will take precedence and start listening for that exact IP on port 800. This is useful for smaller set of scenarios (i.e. for a transient connection where you read/write to an exact IP using a secure channel).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With