read_some / write_some和receive / send之间的区别?

时间:2011-08-12 17:50:32

标签: c++ boost boost-asio

我开始使用Boost Asio的TCP套接字。 read_somereceive之间的区别是什么?write_somesend之间有什么区别?谢谢!

3 个答案:

答案 0 :(得分:10)

据我记忆, read_some 接收实际上也在做同样的事情。我认为接收只是调用read_some,反之亦然。一个命名来自将套接字视为文件(读/写)的想法,而另一个命名则来自连接(发送/接收)点视图。同样适用于 write_some 发送

答案 1 :(得分:5)

BOOST ASIO documentation中, TCP客户端部分说:

  

可以使用以下方法从连接的TCP套接字读取或写入数据   receive(),async_receive(),send()或async_send()成员函数。   但是,因为这些可能导致short writes or reads,   应用程序通常会使用以下操作:   read(),async_read(),write()和async_write()。

答案 2 :(得分:2)

同样的。两者都叫这个 - > get_service()。send()

 /// Send some data on the socket.
 /**
 * This function is used to send data on the stream socket. The function
 * call will block until one or more bytes of the data has been sent
 * successfully, or an until error occurs.
 *
 * @param buffers One or more data buffers to be sent on the socket.
 *
 * @returns The number of bytes sent.
 *
 * @throws boost::system::system_error Thrown on failure.
 *
 * @note The send operation may not transmit all of the data to the peer.
 * Consider using the @ref write function if you need to ensure that all data
 * is written before the blocking operation completes.
 *
 * @par Example
 * To send a single data buffer use the @ref buffer function as follows:
 * @code
 * socket.send(boost::asio::buffer(data, size));
 * @endcode
 * See the @ref buffer documentation for information on sending multiple
 * buffers in one go, and how to use it with arrays, boost::array or
 * std::vector.
 */
 template <typename ConstBufferSequence>
 std::size_t send(const ConstBufferSequence& buffers)
 {
   boost::system::error_code ec;
   std::size_t s = this->get_service().send(
   this->get_implementation(), buffers, 0, ec);
   boost::asio::detail::throw_error(ec, "send");
   return s;
 }

////////////////////////////////////////////
template <typename ConstBufferSequence>
std::size_t write_some(const ConstBufferSequence& buffers)
{
  boost::system::error_code ec;
  std::size_t s = this->get_service().send(
    this->get_implementation(), buffers, 0, ec);
  boost::asio::detail::throw_error(ec, "write_some");
  return s;
}
来自basic_stream_socket.hpp的