Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ socket programming: maximize throughput/bandwidth on localhost (I only get 3 Gbit/s instead of 23GBit/s)
    text
    copied!<p>I want to create a C++ server/client that maximizes the throughput over TCP socket communication on my localhost. As a preparation, I used <a href="http://sourceforge.net/projects/iperf/" rel="nofollow">iperf</a> to find out what the maximum bandwidth is on my i7 MacBookPro.</p> <pre><code>------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 256 KByte (default) ------------------------------------------------------------ [ 4] local 127.0.0.1 port 5001 connected with 127.0.0.1 port 51583 [ 4] 0.0-120.0 sec 329 GBytes 23.6 Gbits/sec </code></pre> <p>Without any tweaking, ipref showed me that I can reach at least 23.2 GBit/s. Then I did my own C++ server/client implementation, you can find the full code here: <a href="https://gist.github.com/1116635" rel="nofollow">https://gist.github.com/1116635</a></p> <p>I that code I basically transfer a 1024bytes int array with each read/write operation. So my send loop on the server looks like this:</p> <pre><code> int n; int x[256]; //fill int array for (int i=0;i&lt;256;i++) { x[i]=i; } for (int i=0;i&lt;(4*1024*1024);i++) { n = write(sock,x,sizeof(x)); if (n &lt; 0) error("ERROR writing to socket"); } </code></pre> <p>My receive loop on the client looks like this:</p> <pre><code>int x[256]; for (int i=0;i&lt;(4*1024*1024);i++) { n = read(sockfd,x,((sizeof(int)*256))); if (n &lt; 0) error("ERROR reading from socket"); } </code></pre> <p>As mention in the headline, running this (compiled with -O3) results in the following execution time which is about 3 GBit/s:</p> <pre><code>./client 127.0.0.1 1234 Elapsed time for Reading 4GigaBytes of data over socket on localhost: 9578ms </code></pre> <p>Where do I loose the bandwidth, what am I doing wrong? Again, the full code can be seen here: <a href="https://gist.github.com/1116635" rel="nofollow">https://gist.github.com/1116635</a></p> <p>Any help is appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload