Netduino home hardware projects downloads community

Jump to content

The Netduino forums have been replaced by new forums at This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

How-to: Optimize TCP buffer size for maximum throughput

  • Please log in to reply
No replies to this topic

#1 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 03 July 2015 - 06:52 PM

Netduino.IP is designed to work even on tiny MCUs with small amounts of memory.

Because of this, we have set the default transmit and receive buffers to ~1460 bytes. This keeps sockets' RAM usage down to as little as ~3KB each while still permitting full usage of standard-size TCP messages.

But of course the Internet has latency. When you request a web page, it can take around 100ms (estimated average) for each TCP message to arrive. Each message is then ACK'd which can take another 100ms to cross back over the Internet. That can limit Internet-based TCP throughput to about 7300 bytes per second (1460 bytes times five "DATA+ACK round-trips" per second).

For most NETMF applications this is a good default. For others you'll want faster throughput. And for some tight-memory applications you may even want to reduce the buffer size (to the minimum of 536 bytes as recommended/required by the TCP/IP spec).

Here is how you change the size of your receive and send buffers:
// example: set "socket"'s receive buffer size to "rxBufferSize" bytes in length
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, rxBufferSize);

// example: set "socket"'s send buffer size to "txBufferSize" bytes in length
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, txBufferSize);
Of course there are other throughput limitations on the Internet. Most notably, a web server's send buffer size (or other connection management algorithms) will limit the amount of data it can send to your Netduino before requiring an ACK.

In my experience, 8KB is a good receive buffer size for across-the-board performance:
// performance-optimized buffer size (for web requests)
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 8192);
And if you're running a mini web server, 8KB is a good transmit buffer size. But remember that this is per-socket, so if you serve multiple clients this could eat up memory quickly:
// performance-optimized buffer size (for mini web servers)
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 8192);

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.