对于一个socket 你一次性投递多少个IO请求?

22 views
Skip to first unread message

WL38

unread,
Mar 27, 2006, 3:26:38 AM3/27/06
to 高性能网络编程邮件列表
对于一个 socket
如果一次性投递若干个异步IO请求,显然是不能保证这些请求被处理的顺序的。我在
CSDN 上看到一位朋友是这样描述的:

http://topic.csdn.net/t/20050119/12/3737501.html

-----------------------------------------------------------------------------
在一个连接上,我投递一个WSASend发送1K的数据出去,这时候下面的缓冲区里却只剩下512字节的空间,这时候,内核就会把这1K数据里的512字节拷贝到缓冲区里发出去,然后在GetQueueCompleteStatus里返回TRUE,并且在参数里指明只发送了512字节,这时候我们需要继续发送剩下的512字节才能保证把整个1K的数据发送完成,如果在GetQueueCompleteStatus返回后,我们继续发送剩下的512字节的这一个间隙,,刚好另外一个线程投递了一个WSASend发送1K的数据,而这时候下面的缓冲区空了,这1K的数据将被完全发送出去,然后内核才调度到我们继续发送剩下的512字节的这个线程,那么在客户端收到的数据就成了这样:

512字节+1K字节+512字节,而实际上我们希望客户端收到的数据是:
1K(第一次发送的)+1K(第二次WSASend)发送的。

同样,如果多次在一个连接上调用WSARecv,也会出现数据错乱的问题。
使用异步IO的时候,最好能保证上一个IO彻底完成后才继续发出下一个IO。
-----------------------------------------------------------------------------

显然如果需要一次性投递多个请求,那么就要设计一种很好的机制避免这样的问题。问题就在于这个机制的开销会不会比单一投递来的大,那这样的话,多次投递的好处就没有了。不知道哪位做过或者考虑过的朋友谈谈看。

这里有一个例子,他就是允许多个投递的:
网址:http://www.lenholgate.com/
代码:http://www.lenholgate.com/archives/zips/SocketServers-LatestRelease.zip

sunway

unread,
Mar 27, 2006, 8:03:38 PM3/27/06
to 高性能网络编程邮件列表
呵呵,我一次只投递一个。

burns7975

unread,
Mar 28, 2006, 8:20:00 AM3/28/06
to 高性能网络编程邮件列表
hehe,永远不会这样的!!

Collins

unread,
Mar 28, 2006, 8:22:43 PM3/28/06
to dev4s...@googlegroups.com
以下文字摘录自:MSDN - Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports

As just mentioned, AFD.SYS handles buffer management for applications that use Winsock to talk to the transport protocol drivers. This means that when an application calls the send or WSASend function to send data, the data gets copied by AFD.SYS to its internal buffers (up to the SO_SNDBUF setting) and the send or WSASend function returns immediately. The data is then sent by AFD.SYS behind the application's back, so to speak. Of course, if the application wants to issue a send for a buffer larger than the SO_SNDBUF setting, the WSASend call blocks until all the data is sent.

Similarly, on receiving data from the remote client, AFD.SYS will copy the data to its own buffers as long as there is no outstanding data to receive from the application, and as long as the SO_RCVBUF setting is not exceeded. When the application calls recv or WSARecv, the data is copied from AFD.SYS's buffers to the application-provided buffer.
Reply all
Reply to author
Forward
0 new messages