hopeful
Member
hi,
I am confused about the operation of the BeginRead and EndRead methods with respect to the way the buffer parameters in the BeginRead method are used.
Firstly, there appears to be a serious problem in the code sample for the EndRead method (appears in version 2 and in 3.5 possible all versions).
This has been noticed before (see link below):
http://www.vbdotnetforums.com/net-sockets/14938-can-someone-explain-code-snippet-msdn-networkstream-endread.html
If you take the code examples for the BeginRead and EndRead methods together (and they appear to be intended to fit together), then my second point of confusion is regarding how the buffer parameter in the BeginRead parameter is used:
1. Within the callback function BeginRead is called within a loop. It populates the local myReadBuffer each time, but doesn't do anything with the data that is presumably placed into myReadBuffer.
2. Each call to BeginRead (in 1 above) is followed by a callback to EndRead, but EndRead doesn't have access to the buffer that was passed to the corresponding BeginRead call (or if it does the callback function in the code example certainly isn't using it).
3. So it appears that any data read by the loop within the callback function will be lost.
4. Furthermore, what data ends up in the buffer that was passed to the initial call to BeginRead? Even though there were multiple calls to BeginRead within the callback function (each populating a different buffer), does the buffer passed to the initial BeginRead call somehow magically get populated with ALL the data that was read (by the multiple calls to BeginRead within the callback function)? If so how does this happen? If not then what purpose does the buffer passed to the initial BeginRead serve?
In other code examples I have seen the initial BeginRead call and the BeginRead call within the callback function are passed the same buffer variable. This seems to me to be the only way for the methods to be used that makes any sense, in which case within the BeginRead loop in in the callback function you would have to be careful to copy the data out before each call to BeginRead otherwise it would get overwritten. ADDED: or you would have to pass a different offset value to each BeginRead call so that the buffer contained all the data after all BeginRead calls had completed.
I find this very confusing. Am I missing something? I'd be interested to read comments on this.
Thanks.
I am confused about the operation of the BeginRead and EndRead methods with respect to the way the buffer parameters in the BeginRead method are used.
Firstly, there appears to be a serious problem in the code sample for the EndRead method (appears in version 2 and in 3.5 possible all versions).
This has been noticed before (see link below):
http://www.vbdotnetforums.com/net-sockets/14938-can-someone-explain-code-snippet-msdn-networkstream-endread.html
If you take the code examples for the BeginRead and EndRead methods together (and they appear to be intended to fit together), then my second point of confusion is regarding how the buffer parameter in the BeginRead parameter is used:
1. Within the callback function BeginRead is called within a loop. It populates the local myReadBuffer each time, but doesn't do anything with the data that is presumably placed into myReadBuffer.
2. Each call to BeginRead (in 1 above) is followed by a callback to EndRead, but EndRead doesn't have access to the buffer that was passed to the corresponding BeginRead call (or if it does the callback function in the code example certainly isn't using it).
3. So it appears that any data read by the loop within the callback function will be lost.
4. Furthermore, what data ends up in the buffer that was passed to the initial call to BeginRead? Even though there were multiple calls to BeginRead within the callback function (each populating a different buffer), does the buffer passed to the initial BeginRead call somehow magically get populated with ALL the data that was read (by the multiple calls to BeginRead within the callback function)? If so how does this happen? If not then what purpose does the buffer passed to the initial BeginRead serve?
In other code examples I have seen the initial BeginRead call and the BeginRead call within the callback function are passed the same buffer variable. This seems to me to be the only way for the methods to be used that makes any sense, in which case within the BeginRead loop in in the callback function you would have to be careful to copy the data out before each call to BeginRead otherwise it would get overwritten. ADDED: or you would have to pass a different offset value to each BeginRead call so that the buffer contained all the data after all BeginRead calls had completed.
I find this very confusing. Am I missing something? I'd be interested to read comments on this.
Thanks.
Last edited: