> I expect to get a data record of N byte length. I'd like to wait
> until the whole record has been received then I can call Receive()
> to read it back. Otherwise, if a OnDataAvailable() is triggered
> before the whole record is received, I will have to temporarily
> store it somewhere and concatenate the data.
> If RcvdCount is not reliable, I can probably use PeekData() but
> this is not very efficient because the data might be copied twice
> each by PeedData() and Receive();

Do not use RcvdCount and do not peak into data. Simply receive data into a
buffer having the size of your record. Have a variable with the count of
already received byte. Of course you initialize this variable to 0.
Then when onDataAvailable call Receive, passing the buffer pointer
incremented by the count of already received bytes and the length of the
buffer minus the already received byte count. Receive will return the length
of data received (or -1 in that case you simply ignore it, or 0 meaning the
remote has properly closed the connection). Add this received data length to
your received byte count. If the sum less than the record size, then do
nothing more since data will come by itself in the next dataavailable. If
the count is equal to the record size, then you have your record. Clear the
received byte count and do whatever is needed once you have your record. If
the count is greater than the record size, then you have a bug.

The actual code is probably smaller than my explanation :-)

Contribute to the SSL Effort. Visit http://www.overbyte.be/eng/ssl.html
Author of ICS (Internet Component Suite, freeware)
Author of MidWare (Multi-tier framework, freeware)

To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to