WSARecv 和 WSABuf 问题

发布于 2024-10-17 09:08:37 字数 293 浏览 3 评论 0 原文

我对 WSARecv 问题 1 有几个问题

: 如何准确确定必须分配给 WSABUF.len 的大小?

问题2: WSARecv 是否读取长度为 0 的数据?

例如。 WSABUF.len = 0

我想用它来确定是否所有数据包都已发送。

问题3: 如果我理解正确,lpNumberOfBytesRecvd 保存实际读取的字节数,而不是 WSABUF.len,对吗?

谢谢。

I have a few questions regarding WSARecv

Question 1:
How do I excactly determine what size we must assign to the WSABUF.len?

Question 2:
Does WSARecv read data with length 0?

eg.
WSABUF.len = 0

I want to use that to use that to determine whether all packets are sent.

Question 3:
If I understand this correctly the lpNumberOfBytesRecvd holds the number of bytes actually read and not WSABUF.len, correct?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2024-10-24 09:08:37

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 that WSABUF.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文