我正在阅读 Boost Asio 的文档,它说
boost::asio::async_write_some
may NOT transfer all of the data to the peer. Consider using theasync_write
function if you need to ensure that all data is written before the asynchronous operation completes.
所以这是我的问题,在什么情况下我们应该使用它们,确保所有数据都写入不是很重要吗?!何时使用 async_write_some 只是这个函数在我看来没什么用?
最佳答案
当您需要发送完全适合缓冲区的特定响应时,
async_write
在请求-回复模式下非常有用。 IE。消息发送后切换到阅读模式时。
但是,如果您需要发送的数据多于写入缓冲区中的数据,那么是否发送整个缓冲区并不重要,因为您还有更多数据要发送。在这种情况下,确保发送整个缓冲区的效率将会很低,因为最后一个 block 可能比您在循环中调用 async_write_some
时要小的 block ,用更多数据填充缓冲区当你去时。为了获得最佳性能,您需要确保发送缓冲区中始终有足够的数据来填充整个帧。
关于c++ - 当使用 async_write_some 和 async_write 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47366762/