c++ - 当使用 async_write_some 和 async_write 时

标签 c++ boost boost-asio

我正在阅读 Boost Asio 的文档,它说

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.

所以这是我的问题,在什么情况下我们应该使用它们,确保所有数据都写入不是很重要吗?!何时使用 async_write_some 只是这个函数在我看来没什么用?

最佳答案

当您需要发送完全适合缓冲区的特定响应时,

async_write 在请求-回复模式下非常有用。 IE。消息发送后切换到阅读模式时。

但是,如果您需要发送的数据多于写入缓冲区中的数据,那么是否发送整个缓冲区并不重要,因为您还有更多数据要发送。在这种情况下,确保发送整个缓冲区的效率将会很低,因为最后一个 block 可能比您在循环中调用 async_write_some 时要小的 block ,用更多数据填充缓冲区当你去时。为了获得最佳性能,您需要确保发送缓冲区中始终有足够的数据来填充整个帧。

关于c++ - 当使用 async_write_some 和 async_write 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47366762/

相关文章:

c++ - vector (反)序列化与 Boost.serialization 的向后兼容性

C++ Boost ASIO async_send_to 内存泄漏

c++ - asio::async_write 和 strand

boost-asio - 使用 boost asio 枚举我的卡的 ipv4 和 ipv6 地址

c++ - 右值和左值引用

c++ - 是否分配给临时对象未定义行为的字段?

c++ - 当我运行 cxxtest 时,出现 undefined reference 错误

c++ - Boost 为结构分配 list_of

c++ - 与 boost .lib 链接

c++ - 带有非常量参数的 boost::bind 方法