Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the practical use of Socket.ExclusiveAddressUse?

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?

like image 567
Kongress Avatar asked Oct 10 '22 16:10

Kongress


1 Answers

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).

like image 178
Teoman Soygul Avatar answered Oct 13 '22 10:10

Teoman Soygul