title: How to determine stream end in a asynchronous socket server
body: <p>I’m trying to write a asynchronous socket in c#. I read a lot of msdn articles and found those two examples: <a href="http://msdn.microsoft.com/en-us/library/fx6588te%28v=vs.85%29.aspx" rel="nofollow">server</a>, <a href="http://msdn.microsoft.com/en-us/library/bew39x2a%28v=vs.85%29.aspx" rel="nofollow">client</a>.</p>
  
  <p>I understood the sample code an use this to implement my own async socket. In the example the server checks for <code>&lt;EOF&gt;</code> to determine the end of the stream and send a response. I would like to know without checking for a special literal when the stream ends. My idea was to check <code>(bytesRead &gt; 0)</code> and call <code>handler.BeginReceive()</code> recursively.
  See following:</p>
  
  <blockquote>
    <p>Original</p>
  </blockquote>
  
  <pre><code>if (bytesRead &gt; 0) {
      // There  might be more data, so store the data received so far.
      state.sb.Append(Encoding.ASCII.GetString(
          state.buffer,0,bytesRead));
  
      // Check for end-of-file tag. If it is not there, read 
      // more data.
      content = state.sb.ToString();
      if (content.IndexOf("&lt;EOF&gt;") &gt; -1) {
          // All the data has been read from the 
          // client. Display it on the console.
          Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
              content.Length, content );
          // Echo the data back to the client.
          Send(handler, content);
      } else {
          // Not all data received. Get more.
          handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
          new AsyncCallback(ReadCallback), state);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>My idea</p>
  </blockquote>
  
  <pre><code>if (received &gt; 0)
  {
      state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, received));
      handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
  }
  else
      Send(handler, state.sb.ToString());
  </code></pre>
  
  <p>If I replace the original part with my part the program stops running.
  I guess the client execute <code>EndSend()</code> in <code>SendCallback()</code> and block the Thread after <code>receiveDone.WaitOne()</code> in <code>StartClient()</code>. How could I pass this? Is it necessary to use a token which determine the end of the stream?</p>
  
  <p>Are there any other good example codes? I just found those two.</p>
  
  <p>(Evidence. The server should receive online clients and put them into a circulate buffer from this buffer he should read and process the records with multithreads. )</p>
  
  <p><strong>Edit</strong></p>
  
  <p>If I use folling:</p>
  
  <pre><code>if (receive &gt; 0)
      state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, receive));
  if (receive == StateObject.BufferSize)
      state.listener.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
  else
      Send(state.listener, state.sb.ToString());
  </code></pre>
  
  <p>Everyting works fine, I guess. Is it ok? Or miss I something? </p>
  
  <p>If I combine the two ifs, it doesn’t work again. Why?</p>
  
  <p><code>if(receive &gt; 0 || receive == StateObject.BufferSize)</code> -> <code>if(receive &gt; 0) // Not working.</code></p>