title: Tcp buffering breakage in .NET runtime
body: <p>This is an old problem I abandoned awhile ago because I could fine no solution and it only affected one server (so I just put my service somewhere else).  <a href="https://stackoverflow.com/questions/8990328/net-tcp-server-receives-bytes-in-large-clumps-every-few-minutes">This</a> user has a problem with identical symptoms to mine:</p>
  
  <blockquote>
    <ul>
    <li>C# .net synchronous Tcp server</li>
    <li>a TcpClient object is assigned by blocking on a TcpListener with the AcceptTcpClient method</li>
    <li>once there's a TcpClient object, I pass it to a thread that invokes the client's GetStream method to create a NetworkStream</li>
    <li>this NetworkStream is looped over, in each iteration doing a networkStream.Read(someBuffer, 0, 4096)</li>
    <li>right now client and server are located on the same network, with no congestion to speak of</li>
    <li>my server has plenty of memory to spare</li>
    <li><strong>if I load my server software onto another machine, the problem goes away</strong></li>
    <li>the kicker: <em>traffic from a network Linux box gets through fine and on time</em></li>
    </ul>
  </blockquote>
  
  <p>The <code>tcpClient.AcceptTcpClient()</code> method blocks for around a minute at a time, resulting in the server having to read a huge block of bytes a short while later, instead of what it should do.  It should do networkStream.Read() small blocks of bytes as frequently as they are sent (and the client sends them every 5s, not once a minute).</p>
  
  <p>Previous comments to the other user suggest subpar networking or connectivity issues might be to blame, which at first seems reasonable.  But this isn't actually the case.</p>
  
  <p>I went one step further and installed packet analyzers at both the client and server.  Results:</p>
  
  <ul>
  <li>the <em>instant</em> the client sends one it shows up on the server's analyzer</li>
  <li>network latency or connectivity are NOT the problem</li>
  <li>the packet/frame are arriving at server at the correct time</li>
  <li>somewhere between the network interface card that my analyzer is monitoring and my application something is causing this delay</li>
  <li><strong>the .NET runtime is the only thing between my application and network interfacing</strong></li>
  <li><strong>some kind of socket error in .NET is the cause of this huge latency</strong></li>
  </ul>
  
  <p>Environment:</p>
  
  <ul>
  <li>in my specific case I'm using a Intel PRO/1000 MT Network card, and .NET</li>
  <li>Standard Edition Server 2003 R2, SP2</li>
  <li>.NET Frameworks installed: 2.0 SP2, 3.0 SP2, 3.5 SP1, 4 Client Profile, 4 Extended</li>
  </ul>
  
  <p>If anyone has any advice I would very much like to know what it is.</p>