title: Understanding the NetworkStream.EndRead()-example from MSDN
body: <p>I tried to understand the MSDN example for NetworkStream.EndRead(). There are some parts that i do not understand.</p>
  
  <p>So here is the example (copied from <a href="http://msdn.microsoft.com/de-de/library/system.net.sockets.networkstream.endread.aspx?cs-save-lang=1&amp;cs-lang=csharp#code-snippet-2" rel="noreferrer">MSDN</a>):</p>
  
  <pre><code>// Example of EndRead, DataAvailable and BeginRead.
  
  public static void myReadCallBack(IAsyncResult ar ){
  
      NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
      byte[] myReadBuffer = new byte[1024];
      String myCompleteMessage = "";
      int numberOfBytesRead;
  
      numberOfBytesRead = myNetworkStream.EndRead(ar);
      myCompleteMessage = 
          String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));    
  
      // message received may be larger than buffer size so loop through until you have it all.
      while(myNetworkStream.DataAvailable){
  
          myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, 
                                                 new AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack), 
                                                     myNetworkStream);  
  
      }
  
      // Print out the received message to the console.
      Console.WriteLine("You received the following message : " +
                                  myCompleteMessage);
  }
  </code></pre>
  
  <p>It uses BeginRead() and EndRead() to read asynchronously from the network stream.
  The whole thing is invoked by calling </p>
  
  <pre><code>myNetworkStream.BeginRead(someBuffer, 0, someBuffer.Length, new AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack), myNetworkStream);  
  </code></pre>
  
  <p>somewhere else (not displayed in the example).</p>
  
  <p>What I think it should do is print the whole message received from the NetworkStream in a single WriteLine (the one at the end of the example). Notice that the string is called <code>myCompleteMessage</code>.</p>
  
  <p>Now when I look at the implementation some problems arise for my understanding.</p>
  
  <p>First of all: The example allocates a new method-local buffer <code>myReadBuffer</code>. Then EndStream() is called which writes the received message into the buffer that BeginRead() was supplied. This is NOT the <code>myReadBuffer</code> that was just allocated. How should the network stream know of it? So in the next line <code>numberOfBytesRead</code>-bytes from the empty buffer are appended to <code>myCompleteMessage</code>. Which has the current value <code>""</code>. In the last line this message consisting of a lot of <code>'\0'</code>s is printed with <code>Console.WriteLine</code>.</p>
  
  <p>This doesn't make any sense to me.</p>
  
  <p>The second thing I do not understand is the <code>while</code>-loop.</p>
  
  <p><code>BeginRead</code> is an asynchronous call. So no data is immediately read. So as I understand it, the while loop should run quite a while until some asynchronous call is actually executed and reads from the stream so that there is no data available any more. The documentation doesn't say that <code>BeginRead</code> immediately marks some part of the available data as being read, so I do not expect it to do so.</p>
  
  <p>This example does not improve my understanding of those methods. Is this example wrong or is my understanding wrong (I expect the latter)? How does this example work?</p>