title: c# Async Socket.BeginSendFile only sends two files async
body: <p>I am puzzled at why my <strong>Socket.BeginSendFile</strong> only sends two files async at a time.  I have tested on different computer with the same results.</p>
  
  <p><strong><em>Socket.BeginSend</strong> seems to send all three at once as expected.</em></p>
  
  <p>I tested connecting three clients to the server. Which works great, all three connect async as expected.  Each client (Test100.exe, Test200.exe, Test300.exe in picture) will request a file from either a folder named 100, 200 or 300 from the server depending on the which client connected.</p>
  
  <p>The problem is that <strong>Socket.BeginSendFile</strong> is only sending max two files at a time onto the stream.  As soon as one of the 2 previously called <strong>Socket.BeginSendFile</strong> have finished. See below;
  <img src="https://i.stack.imgur.com/yrmwa.png" alt="enter image description here"></p>
  
  <p><strong>As soon as one of the first two clients sending finishes the send, the third begins receiving.</strong></p>
  
  <p><img src="https://i.stack.imgur.com/GLqZv.png" alt="enter image description here"></p>
  
  <p>Putting some breakpoints into the code, I can tell that all three Socket.BeginSendFile(...) are being called async.</p>
  
  <pre><code>    private static void Send(Socket handler, String data)
      {
          string dateFolder =  data.Replace("&lt;EOF&gt;", "");
  
          string longFileName = "C:\\"+dateFolder+"\\poopoo.txt";
          string shortFileName = "poopoo.txt";
  
          // ==== for beginSend
          //byte[] fileData = File.ReadAllBytes(longFileName);
          // ==== for beginSend
  
  
          byte[] fileNameByte = Encoding.ASCII.GetBytes(shortFileName);
          byte[] fileInfo = Encoding.ASCII.GetBytes("C:\\Users\\Trim\\Desktop");
          byte[] fileInfoLen = BitConverter.GetBytes(fileInfo.Length); // we know these are ints (4bytes)
  
          byte[] clientData = new byte[4 + fileNameByte.Length + fileInfoLen.Length + fileInfo.Length];// + fileData.Length + eofByte.Length
          // ==== for beginSend
          //byte[] clientData = new byte[4 + fileNameByte.Length + fileInfoLen.Length + fileInfo.Length + fileData.Length];
          // ==== for beginSend
  
          byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length); // we know these are int (4bytes);
  
          fileNameLen.CopyTo(clientData, 0);
          fileNameByte.CopyTo(clientData, 4); // room for error file name 4bytes?
          fileInfoLen.CopyTo(clientData, 4 + fileNameByte.Length);
          fileInfo.CopyTo(clientData, 4 + fileNameByte.Length + fileInfoLen.Length);
  
          // ==== for beginSend
          //fileData.CopyTo(clientData, 4 + fileNameByte.Length + fileInfoLen.Length + fileInfo.Length);
          // ==== for beginSend
          // *** Break point shows all three being called async
          handler.BeginSendFile(longFileName, clientData, null, 0, new AsyncCallback(AsynchronousFileSendCallback), handler);
  
  
  
         // handler.BeginSend(clientData, 0, clientData.Length, 0, new AsyncCallback(SendCallback), handler);
  
  
      }
  </code></pre>
  
  <p>The third Socket.BeginSendFile(...) doesnt actually begin to send the file until one of the first two AsynchronousFileSendCallback methods </p>
  
  <pre><code>    private static void AsynchronousFileSendCallback(IAsyncResult ar)
      {
          // Retrieve the socket from the state object.
          Socket client = (Socket)ar.AsyncState;
  
          // Complete sending the data to the remote device.
          client.EndSendFile(ar); // **Third** client doesn't actually start receiving his data until EndSendFile is called atleast once.
  
          Console.WriteLine("Send file to client.");
         // sendDone.Set();
          client.Shutdown(SocketShutdown.Both);
          client.Close();
      }
  </code></pre>
  
  <p>As I said earlier, if I use <strong>Socket.BeginSend</strong> the issue goes away.  Although I need to be able to use <strong>Socket.BeginSendFile</strong> because it chunks the file on a separate thread and particularly this line of code <code>byte[] fileData = File.ReadAllBytes(longFileName);</code> used with my <strong>Socket.BeginSend</strong> structure is unacceptable for large files :(</p>
  
  <p>Thank you all very much for your time!</p>