When using a blocking TCP socket, I should not indicate the size of the buffer. For example:
using (var client = new TcpClient()) { client.Connect(ServerIp, ServerPort); using (reader = new BinaryReader(client.GetStream())) using (writer = new BinaryWriter(client.GetStream())) { var byteCount = reader.ReadInt32(); reader.ReadBytes(byteCount); } }
Notice how the remote host could send any number of bytes.
However, when using asynchronous TCP sockets, I need to create a buffer and, therefore, specify the maximum size of the hard code:
var buffer = new byte[BufferSize]; socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, callback, null);
I could just set the buffer size, say, 1024 bytes. This will work if I need to get small chunks of data. But what if I need to get a 10 MB serialized object? I could set the buffer size to 10 * 1024 * 1024 ... but that would lose a constant 10 MB of RAM while the application was running. This is silly.
So my question is: How can I efficiently receive large chunks of data using asynchronous TCP sockets?
asmo
source share