# .NET平台下几种SOCKET模型的简要性能供参考的讨论【转】

### IOCP（完成端口）

posted on 2006-01-05 10:13 天生这样 阅读(7516) 评论(21)  编辑 收藏 所属分类: C#

### 评论

#### #1楼 2006-01-05 11:26 风满袖

Socket ＋ ThreadPool 和Socket + Asynchronous本质上是一样的，性能自然也没有什么差别。   回复  引用  查看

回复  引用  查看

#### #3楼 2006-01-05 12:58 双鱼座

.net上的完成端口实现很多呀，在CodeProject上随便一搜就是一大把。例如那个Sonic.net好象比较有名：http://www.codeproject.com/csharp/managediocp.asp

)，不具有广泛的兼容性，更别说Linux/Mono了。并且大部分情况下，托管代码都是通过AppServer(例如IIS)获取Socket服务端连接，自己写纯托管的Socket服务端好象没有多大必要。   回复  引用  查看

#### #4楼[楼主] 2006-01-05 13:11 天生这样

Managed I/O Completion Ports这个我看过，我想应该算是伪IOCP

回复  引用  查看

#### #6楼 2006-01-05 15:16 Sumtec

Depends On
Used By
System.Net.Sockets.OverlappedCache..ctor(Overlapped, Object, IOCompletionCallback, Boolean)
Depends On
Used By
System.Net.Sockets.BaseOverlappedAsyncResult.SetUnmanagedStructures(Object) : Void
Depends On
Used By
System.Net.Sockets.OverlappedAsyncResult.SetUnmanagedStructures(Byte[], Int32, Int32, SocketAddress, Boolean) : Void
Depends On
Used By
System.Net.Sockets.OverlappedAsyncResult.SetUnmanagedStructures(Byte[], Int32, Int32, SocketAddress, Boolean, OverlappedCache&) : Void
Depends On
Used By
System.Net.Sockets.Socket.DoBeginReceive(Byte[], Int32, Int32, SocketFlags, OverlappedAsyncResult) : SocketError
System.Net.Sockets.Socket.DoBeginSend(Byte[], Int32, Int32, SocketFlags, OverlappedAsyncResult) : SocketError
System.Net.Sockets.Socket.DoBeginSendTo(Byte[], Int32, Int32, SocketFlags, EndPoint, SocketAddress, OverlappedAsyncResult) : Void

#### #7楼 2006-01-05 15:43 双鱼座

Overlap模型是异步Select模型的优化,是一个比IOCP兼容范围广(适宜所有的Windows版本),但性能比IOCP略逊的一种IO模型. 与IOCP模型的共同点是工作者线程回调中处理机制相同，都需要通过Overlap结构来承载数据；区别在于,不会通过调用 CreateIoCompletionPort建立端口,也不会调用GetQueuedCompletionStatus来轮询。

#### #8楼[楼主] 2006-01-05 15:49 天生这样

Winsock中IO模型有五种，这些可以参考相关文档。
回复  引用  查看

#### #9楼 2006-01-05 16:11 ccBoy

A good rule of thumb is to set the concurrency level to match the number of CPU’s in the system. If the machine our server is running on only has one CPU, then only one thread can be executing at any given time. It will require a task swap to have another thread get CPU time. We want to reduce the number of active threads at any given time to maximize performance. This also leads to scalability. As the number of CPU’s increase, we can increase the concurrency level because there is a CPU to execute that thread. This is a general rule and is always a good starting point for configuring our thread pools.
回复  引用  查看

#### #10楼218.2.159.* 2006-01-05 16:26 问 题 男 [未注册用户]

btw：

@双鱼座：
“IOCP是Windows2000/NT特有的Socket模型”——iocp不仅仅可用于socket，一般的磁盘io也可使用

@sumtec：

回复  引用

@问 题 男:

#### #12楼 2006-01-05 17:22 ccBoy

Inside I/O completion ports
http://www.sysinternals.com/Information/IoCompletionPorts.html

回复  引用  查看

#### #13楼 2006-01-05 17:28 Sumtec

@ 所有人：

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/progthrepool.asp

This kind of thread is used for I/O operations, whenever is possible. Windows NT, Windows 2000, and Windows XP offer an object specialized on asynchronous operations, called IOCompletionPort. With the API associated with this object we can launch asynchronous I/O operations managed with a thread pool by the system, in an efficient way and with few resources. However, Windows 95, Windows 98, and Windows Me have some limitations with asynchronous I/O operations. For example, IOCompletionPorts functionality is not offered and asynchronous operations on some devices, such as disks and mail slots, cannot be performed. Here you can see one of the greatest features of the .NET Framework: compile once and execute on multiple systems. Depending on the target platform, the .NET Framework will decide to use the IOCompletionPorts API or not, maximizing the performance and minimizing the resources.

If you run this program on Microsoft Windows NT, Windows 2000, or Windows XP, you will see the following output:

Connected to localhost:80
Request sent to localhost:80
WorkerThreads: 25, CompletionPortThreads: 24

As you can see, connecting with a socket uses a worker thread, while sending the data uses a CompletionPort. This following sequence is followed:

1. We get the local IP address and connect to it asynchronously.
2. Socket performs the asynchronous connection on a worker thread, since Windows IOCompletionPorts cannot be used to establish connections on sockets.
3. Once the connection is established, the Socket class calls the specified function ConnectCallback. This callback shows the number of available threads on the pool, this way we can see that it is being executed on a worker thread.
4. An asynchronous request is sent from the same function ConnectCallback. We use for this the BeginSend method, after encoding the Get / request in ASCII code.
5. Send/receive operations on a socket can be performed asynchronously with an IOCompletionPort, so when our request is done, the callback function SendCallback is executed on a CompletionPortthread. We can check this because the function itself shows the number of available threads and we can see that only those corresponding to CompletionPorts have been decreased.
If we run the same code on a Windows 95, Windows 98, or Windows Me platform, the result will be the same on the connection, but the request will be sent on a worker thread, instead of a CompletionPort. The important thing you should learn about this is that the Socket class always uses the best available mechanism, so you can develop your application without taking into account the target platform.

回复   引用   查看

#### #14楼 2006-01-05 18:46 ccBoy

Ps: Sumtec , 你记忆力真好，而且有信心保持自己的观点，赞一个。
回复  引用  查看

回复  引用  查看

#### #16楼10.171.16.* 2006-01-29 15:26 飞刀.Net [未注册用户]

ccBoy提供了一个aio，我不太清楚，我呆会儿查查．

to 飞刀.Net：

回复  引用  查看

#### #18楼219.133.154.* 2006-06-23 17:36 baoli [未注册用户]

Sonic.net
http://www.codeproject.com/csharp/managediocp.asp

#### #19楼 2006-06-30 18:22 让变化成为计划的一部分

http://devauthority.com/blogs/krys/archive/2006/06/13/HighPerformanceSocket.aspx,
High Performance Socket in .NET - using IOCP Thread & more..

Sonic.net 的问题比较严重，经常会发现，只有一个线程在那里狂转，其他的线程闲着。这个很不符合IOCP的原意。所以，Sonic.net 还只能算是试图模拟IOCP，但模拟的并不完美。   回复  引用  查看

#### #20楼219.134.70.* 2007-01-31 14:42 byjove [未注册用户]

Sonic.net 在pause 时,也不能恢复.....
回复  引用

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

11-12 266

01-08 2940
01-06 5192
10-15 588
05-15 780
04-06 395
01-21 5200
02-24 5350
07-14 1178
08-28 1051