I recently started using the new C#5.0 "async" and "await" keywords. I thought I got the twist but realized one thing that made me doubt. Here's how I was asynchronously receiving data from a remote TcpClient. Once I accept the connection I call this function :
static async void ReadAsync(TcpClient client)
NetworkStream ns = client.GetStream();
MemoryStream ms = new MemoryStream();
byte buffer = new byte;
int bytesRead = await ns.ReadAsync(buffer, 0, buffer.Length);
ms.Write(buffer, 0, bytesRead);
I ended up using length-prefixed messages, prefixing every packet by 4 bytes representing the length of the data to come.