to zhanshenpkl(LittlePK)
in your answer :"我经过测试,如果我在OnReceive()函数的最后调用AsyncSelect(FD_WRITE),而在OnSend()函数的最后调用AsyncSelect(FD_READ),那么就会不断的循环。"
应该是OnReceive()后调用AsyncSelect(FD_READ)吧?
To vagabondkq(birder):
我把MSDN里关于WSAAsyncSelect()函数的一段英文摘出来大家看看:
The WS2_32.DLL will not continually flood an application with messages for a particular network event. Having successfully posted notification of a particular event to an application window, no further message(s) for that network event will be posted to the application window until the application makes the function call that implicitly reenables notification of that network event.
如果你想再发送数据就再次调用Select语句,那么当系统准备好(我的理解是它的缓冲区的数据已经全部发送出去了,可以再接受新的数据)就会再次触发FD_WRITE事件。
再看下面一段英文:
An FD_WRITE message is posted when a socket is first connected with connect or WSAConnect (after FD_CONNECT, if also registered) or accepted with accept or WSAAccept, and then after a send operation fails with WSAEWOULDBLOCK and buffer space becomes available. Therefore, an application can assume that sends are possible starting from the first FD_WRITE message and lasting until a send returns WSAEWOULDBLOCK. After such a failure the application will be notified that sends are again possible with an FD_WRITE message.
在连接成功后,就会触发一个FD_WRITE事件。
综合这两句话,如果你不再次调用Select的话,那么它只会触发在连接之后那次FD_WRITE。
case FD_ACCEPT: 表示现在已经有连接被listen到了,可以执行Accept操作了
……
case FD_READ: 表示现在已经有数据在缓冲区了,可以执行recv操作了
……
case FD_WRITE: 表示现在缓冲区已经空闲了,可以执行send操作了
……
case FD_CLOSE: 表示现在有连接断开了
……
}