简体   繁体   中英

When use async_write_some and async_write

I was reading the documentation of Boost Asio and it says that

boost::asio::async_write_some may NOT transfer all of the data to the peer. Consider using the async_write function if you need to ensure that all data is written before the asynchronous operation completes.

So here is my question, in which cases should we use them, isn`t VERY important to ensure that all the data is written?! when to use async_write_some just this function seems to me useless?

async_write is useful in request-reply mode when you need to send a specific response that fully fits in your buffer. Ie when you switch to reading mode after the message is sent.

But if you need to send more data than you have in your write buffer then it doesn't matter if the whole buffer is sent or not, because you still have more data to send. In that case ensuring that the whole buffer is sent is going to be inefficient because the last chunk may be smaller than it could have been if you just called async_write_some in a loop, filling up the buffer with more data as you go. For optimal performance you want to ensure there is always enough data in the send buffer to fill a full frame.

When you are asynchronously reading from one place, and writing to another, you probably just want to shove some bytes to the destination, and want to know when there is room in the buffer to send some more. You only need to use async_write for the last write (when you know there is no more to send)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM