Asynchronous client socket - construct back the byte array Question: I would like to know how to get all data from a stream.
I am sending data from Asynchronous Client Socket's BeginSend method to the BeginReceive method on the client's side. The goal now is to get all the bytes passed over the stream.
I have created a StateObject class, which includes a buffer field type of byte[], and it has a fixed length (currently set to 2048).
So how can I pass the retrieved bytes to this field? Keep in mind that the data is coming in pieces, not all at once.
I have to construct all the bytes back as they were sent from server.
Can I use the Array.Copy() or Buffer.BlockCopy method, will it do the job? 
The point is I have to consider multiple conditions:


*

*lets asume that all the indexes of buffer[2048] are set to 0

*in 1st copying starting index will  be 0 (then add the bytes get from the stream)

*in all next copyings the starting index must the the 1st zero in the array (or said with other words, the sum all all previous bytes copyed to array - 1).
How to achive that?

Every time you get ReadCallback fired you have >0 byte in buffer (otherwise connection is broken) and number of actual bytes received in this data chunk. Every new data chunk starts from index 0 in your buffer.
Use var message = new MemoryStream() and than message.Write(buffer,0, bytes_received)on every read callback until you have all the data received. This will be the fastest way.