WSARecv 和 WSABuf 问题
我对 WSARecv 问题 1 有几个问题
:
如何准确确定必须分配给 WSABUF.len
的大小?
问题2: WSARecv 是否读取长度为 0 的数据?
例如。
WSABUF.len = 0
我想用它来确定是否所有数据包都已发送。
问题3:
如果我理解正确,lpNumberOfBytesRecvd
保存实际读取的字节数,而不是 WSABUF.len
,对吗?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)
WSABUF.len
是您提供的缓冲区的长度。这是此调用中可以读取的最大数量。您知道这个大小,因为它是您为WSABUF.buf
指向的缓冲区分配的内存大小。2) 在某些具有数千个并发连接的高性能情况下,当使用异步 I/O 时,您可以将 WSABUF.len 值设置为 0,以防止读取执行任何操作,除了完成告诉您:数据可用。这是很少需要的性能调整,通常称为“零字节读取”。它有用的原因是它意味着 I/O 系统不需要锁定内存中的读缓冲区(没有读缓冲区),因此这减少了锁定的 I/O 页数。可以锁定的 I/O 页数有固定限制,因此当您有数千个连接但它们不经常发送数据时,这会很有用。您在所有连接上发布零字节读取,然后在零字节读取完成并且您知道有可用数据时发布真实读取。
3)是的。实际读取的字节数通过
lpNumberOfBytesRecvd
单独返回。您可能应该考虑消息框架(我谈论此处 和 此处,因为这将帮助您了解在给定时间您期望从连接读取多少数据。
1)
WSABUF.len
is the length of the buffer that you have supplied. It's the maximum amount that can be read in this call. You know this size as it's the size of the memory that you allocated for the buffer thatWSABUF.buf
points to.2) In certain high performance situations with many thousands of concurrent connections, when using asynchronous I/O you can set the
WSABUF.len
value to 0 to prevent the read from doing anything except completing to tell you that data is available. This is a performance tweak that is rarely needed and is often referred to as a 'zero byte read'. The reason it's useful is that it means that the I/O system doesn't need to lock the read buffer in memory (there is no read buffer) and so this reduces the number of I/O pages locked. There's a fixed limit on the number of I/O pages that can be locked and so this can be useful when you have many thousands of connections but they don't send data very often. You post a zero byte read on all the connections and then post a real read when the zero byte read completes and you know that you have data available.3) Yes. The number of bytes actually read is returned separately via
lpNumberOfBytesRecvd
.You should probably think about message framing (which I talk about here and here as this will help you know how much data you're expecting to read from a connection at a given time.