WSARecv and WSABuf questions
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.
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.
精彩评论