.net的Socket类在使用异步IO模型时会自动使用Windows的完成端口内核对象,在Windows网络编程第二版中是这样描述的:
The asynchronous model in .NET sockets is the best way to manage I/O from one or more sockets. It is the most efficient model of the three because its design is similar to the I/O completion ports model (described in Chapter 5) and on Windows NT–based systems it will use the completion port I/O model internally. Because of this, you can potentially develop a high-performance, scalable Winsock server in C# and even possibly in Visual Basic. For a complete discussion of how to develop a high-performance, scalable Winsock application, see Chapter 6.
Socket类型在.net 2.0版本中更加增强了,一个显著的区别是,.net 2.0中的Socket可以使用DuplicateAndClose来对Socket进行持久化。这个方法的Remarks是这么写的:
If the process creating the socket uses asynchronous methods (BeginReceive or BeginSend), the process must first set the UseOnlyOverlappedIO property to true; otherwise, the socket is bound to the completion port of the creating process, which may cause an ArgumentNullException to be thrown on the target process.
这是因为IOCP是内核对象,持久化是没有意义的,所以Socket类型拥有了一个属性,UseOnlyOverlappedIO ,这个属性的Remarks是这么写的:
Set this property to true for a Socket you intend to call DuplicateAndClose. Otherwise, the Framework may assign a completion port to the socket, which would prohibit the use of DuplicateAndClose.