首页 > 开发 > C++ > 正文

AcceptEx的第3个参数:lpOutputBuffer,应该怎么设置?

2017-09-11 21:35:37  来源: 网友分享

在IOCP模型编程中,因为采用AcceptEx异步接受连接,所以要一次性提交许多AcceptEx,其中第3个参数lpOutputBuffer,看了msdn是用来存放客户端和服务端地址信息等数据的。那么按道理说,一个AcceptEx对应着专属于它的一个buffer,但是看到msdn有段Remark是这样写的:

Using a single buffer improves performance. When using AcceptEx, the GetAcceptExSockaddrs function must be called to parse the buffer into its three distinct parts (data, local socket address, and remote socket address). On Windows XP and later, once the AcceptEx function completes and the SOUPDATEACCEPT_CONTEXT option is set on the accepted socket, the local address associated with the accepted socket can also be retrieved using the getsockname function. Likewise, the remote address associated with the accepted socket can be retrieved using the getpeername function.

无法理解加粗那句话的意思,是指用一个buffer(全局变量)来应付所有的AcceptEx以提高性能吗?

解决方案

这个问题没有上下文就费解了。
在你引用的这段话之前的一句话如下:

A single output buffer receives the data, the local socket address (the server), and the remote socket address (the client).

结合上下文就明了了。
这分明说的是用一个buffer来同时接收data,local socket address和remote socket address,而不用分为三个buffer。通过这样的方法来提高性能。
也正是因为这样一个机制,所以接下来才说需要调用GetAcceptExSockaddrs来解析这三块不同的数据。