title: C++ TCP socket sending speed
body: <p>I'm sending messages to remote server using simple locking TCP socket and the problem I have is that for each message it takes very different time to send it.</p>
  
  <p>And here what I get (some example):</p>
  
  <pre><code>Bytes Sent:  217, Time:  34.3336 usec
  Bytes Sent:  217, Time:   9.9107 usec
  Bytes Sent:  226, Time:  20.1754 usec
  Bytes Sent:  226, Time:  38.2271 usec
  Bytes Sent:  217, Time:  33.6257 usec
  Bytes Sent:  217, Time:  12.7424 usec
  Bytes Sent:  217, Time:  21.5912 usec
  Bytes Sent:  217, Time:  31.1480 usec
  Bytes Sent:  218, Time:  28.3164 usec
  Bytes Sent:  218, Time:  13.0963 usec
  Bytes Sent:  218, Time:  82.8254 usec
  Bytes Sent:  218, Time:  13.0963 usec
  Bytes Sent:  227, Time:  30.7941 usec
  Bytes Sent:  218, Time:  27.9624 usec
  Bytes Sent:  216, Time:   2.1237 usec
  Bytes Sent:  218, Time:  12.3884 usec
  Bytes Sent:  227, Time:  31.1480 usec
  Bytes Sent:  227, Time:  88.4887 usec
  Bytes Sent:  218, Time:  93.0901 usec
  Bytes Sent:  218, Time:   7.7870 usec
  Bytes Sent:  218, Time:  28.3164 usec
  Bytes Sent:  227, Time:  89.5505 usec
  Bytes Sent:  218, Time:  84.2412 usec
  Bytes Sent:  218, Time:  13.8042 usec
  Bytes Sent:  227, Time:  99.4612 usec
  Bytes Sent:  218, Time:  86.0110 usec
  Bytes Sent:  218, Time:  12.3884 usec
  Bytes Sent:  218, Time:  87.7807 usec
  Bytes Sent:  216, Time:   3.5395 usec
  Bytes Sent:  218, Time:   4.6014 usec
  Bytes Sent:  218, Time:  36.1034 usec
  Bytes Sent:  218, Time:  14.8661 usec
  Bytes Sent:  218, Time:  24.0689 usec
  Bytes Sent:  218, Time:  18.0517 usec
  Bytes Sent:  227, Time:  24.4229 usec
  </code></pre>
  
  <p>Does anybody know why this can happen? Why for one message it takes 3 usec to be sent and for other 80 usec?<br>
  And is there any way to fix this? </p>
  
  <p>Note: the main goal I want to archive is to send each message as fast as possible. I don't need anychronious sockets, At least until they work faster.</p>
  
  <p>Some additional details regarding what I do:</p>
  
  <p>C++, Visual Studio 2013</p>
  
  <p>How I open:</p>
  
  <pre><code>...
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  ...
  ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);
  ...
  </code></pre>
  
  <p>How I send and calculate time:</p>
  
  <pre><code>...
  LARGE_INTEGER cT;
  QueryPerformanceCounter(&amp;cT);
  long long dT = cT.QuadPart;
  
  iBytesSent = send(ConnectSocket, msgFinal, msgFinalLen, 0);
  
  QueryPerformanceCounter(&amp;cT);
  dT = cT.QuadPart - dT;
  ...
  </code></pre>
  
  <p>Also I'm listening this socket from other thread, I don't know if this can impact sending or not:</p>
  
  <pre><code>iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
  </code></pre>