Search code examples
c++socketsboosttcpboost-asio

When will boost.asio actually start the transmission and send the data?


boost::asio::ip::tcp::socket uses buffers. I know that when write or async_write is called, the data is just written into the buffers. My question is when will them be actually sent? Or, to be more precise, when will asio actually start the transmission?

From what I know, at least, asio must start the transmission when the buffer is full, so the data can be considered sent in this case. But, what if there's no more data put into the buffer? Will short data stay in the buffer forever? Do I need to call some flush() APIs to tell asio that "There's no more for now. Send the data in the buffer right away!" ? If there are no such APIs, how can I ensure the timeliness of transmission?


Solution

  • Thank user7860670, Jakob Stark and sehe for their comments. To summarize:

    1. Asio has no additional buffer mechanisms beyond those in the OS kernel.
    2. The OS kernel uses Nagle's algorithm normally, which promise that data is sent within a finite time frame.
    3. To improve the timeliness, use TCP_NODELAY, which is available as boost::asio::ip::tcp::no_delay in Asio.