title: Networkstream.Write() Blocking Problem
body: <p>I'm currently testing a managed c# network library I've written and I've come up against an occasional problem. This problem manifests as a very consistent (always within 30ms) 5000ms block on networkstream.write() for perhaps 1% of all send operations. This is in a test environment, all running locally, using the exact same packet size (2MB) each time. On the client end I continuously write the following to a connected networkstream:</p>
  
  <pre><code>tcpClientNetworkStream.Write(headerBytes, 0, headerBytes.Length);
  tcpClientNetworkStream.Write(dataBytes, 0, dataBytes.Length);
  </code></pre>
  
  <p>and on the server end I use an asynchronous read waiting for data. Once data appears I use a while loop over tcpClientNetworkStream.DataAvailable until all data has been received. </p>
  
  <p>I am aware that networkstream.write() can block if the buffers are full but if this is the problem I can't think of a quicker way of clearing them at the server end (Send and Receive buffer sizes are default at 8192 bytes). The fact the block is so consistent seems very odd. My first thought was possibly some form of Thread.Sleep but doing a full project search shows none. If anyone could help shed some light on this issue that would be greatly appreciated.</p>
  
  <p>Marc</p>
  
  <p>edit to add: A hack which seems to make the problem go away is the following (although there is an associated performance hit due to the BlockCopy):</p>
  
  <pre><code>byte[] bytesToSend = new byte[headerBytes.Length + dataBytes.Length];
  Buffer.BlockCopy(headerBytes, 0, bytesToSend, 0, headerBytes.Length);
  Buffer.BlockCopy(dataBytes, 0, bytesToSend, headerBytes.Length, dataBytes.Length);
  tcpClientNetworkStream.Write(bytesToSend, 0, bytesToSend.Length);
  </code></pre>
  
  <p>edit to add2: I've also reproduced the problem by using two asynchronous writes with a thread signal between the two. At the moment the only solution I have is the single write operation as in the above edit.</p>
  
  <p>edit to add3: Ok, another possible fix follows. I'm still interested to know why the successive write occasionally 'blocks' in the way it does.</p>
  
  <pre><code>BufferedStream sendStream = new BufferedStream(tcpClientNetworkStream);
  sendStream.Write(bytesToSend, 0, bytesToSend.Length);
  sendStream.Write(packet.PacketData, 0, packet.PacketData.Length);
  sendStream.Flush();
  </code></pre>
  
  <p>edit to add4: After further extensive testing the solution in 'edit to add3' does not make the problem go away, it just reduces the occurrence to about 0.1% of sends. Much better but far from solved. I will be replacing the asynchronous read with a blocking read next to see if that sorts it, as suggested by PaulF.</p>