title: How to ask the socket to wait for more data to come
body: <p>I am playing with <a href="http://rservecli.codeplex.com/" rel="nofollow">RserveCLI</a> project, which is a .net client communicating with the statistical environment R. The basic idea is sending data/commends between this .NET client and an R session via TCP protocol. </p>
  
  <p>One bug that others and I found is that big data trunk, say over 10k bytes, cannot get transfer successfully. I found the but in the following code snippet:</p>
  
  <pre><code>        // send the commend to R, then R will do some computation and get the data ready to send back
          int toConsume = this.SubmitCommand(cmd, data); 
          var res = new List&lt;object&gt;();
          while (toConsume &gt; 0)
          {
              var dhbuf = new byte[4];
              if (this.socket.Receive(dhbuf) != 4)
              {
                  throw new WebException("Didn't receive a header.");
              }
  
              byte typ = dhbuf[0];
  
              // ReSharper disable RedundantCast
              int dlength = dhbuf[1] + (((int)dhbuf[2]) &lt;&lt; 8) + (((int)dhbuf[3]) &lt;&lt; 16);
  
              // ReSharper restore RedundantCast
              var dvbuf = new byte[dlength];
  
              // BUG: I added this sleep line, without this line, bug occures
              System.Threading.Thread.Sleep(500);
  
              // this line cannot receive the whole data at once
              var received = this.socket.Receive(dvbuf);
              // so the exception throws 
              if (received != dvbuf.Length)
              {
                  var tempR = this.socket.Receive(dvbuf);
                  throw new WebException("Expected " + dvbuf.Length + " bytes of data, but received " + received + ".");
              }
  </code></pre>
  
  <p>The reason is that the .NET code runs too fast and the R side cannot send the data that fast. So the receive line after my inserted Thread.Sleep(500) does not get all the data. If I wait some time there, then it can get all the data. But I don't know how long. </p>
  
  <p>I have some basic idea to deal with the problem, for example, continuously use this.socket.Receive() to get data, but if there is no data there .Receive will block there. </p>
  
  <p>I have little experience in socket programming, so I am asking the best practice for this kind of problem. Thanks!</p>