What is the difference between asio::tcp::socket's async_read_some and async_receive?

淺唱寂寞╮ 提交于 2019-11-29 11:17:43

问题


What is the difference between:

  • boost::asio::tcp::socket::async_read_some()
  • boost::asio::tcp::socket::async_receive()

As far as I can tell their documentation is identical.

Which should I prefer?


回答1:


Their specification in the networking TR2 proposal (5.7.10.2 basic_stream_socket members) is identical too:

On async_receive:

Effects: Calls this->service.async_receive(this->implementation, buffers, 0, handler).

On async_read_some:

Effects: Calls this->service.async_receive(this->implementation, buffers, 0, handler).

So I guess this confirms Jerry's impression.




回答2:


I believe the two are essentially identical. The reason they provide both is to provide interfaces similar to both iostreams (which have a read_some member) and sockets (which have a receive).

As Peter Tseng pointed out, async_receive does also have an overload that accepts socket_base::message_flags, which async_read_some does not.




回答3:


Confirming everyone here with two links:

  1. https://github.com/boostorg/asio/blob/36eef63a9cf8ae609716d76ccb3906ff9769d53a/include/boost/asio/basic_stream_socket.hpp#L558
  2. https://github.com/boostorg/asio/blob/36eef63a9cf8ae609716d76ccb3906ff9769d53a/include/boost/asio/basic_stream_socket.hpp#L834

The code is exactly the same. However async_receive has one overload with message_flags, which notably allows you to specify asio::socket_base::message_peek.



来源:https://stackoverflow.com/questions/2238166/what-is-the-difference-between-asiotcpsockets-async-read-some-and-async-rec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!