简体   繁体   English

read_some/write_some 和接收/发送之间的区别?

[英]Difference between read_some/write_some and receive/send?

I am beginning to work with Boost Asio's TCP sockets.我开始使用 Boost Asio 的 TCP sockets。 What is the difference between read_some and receive and what is the difference between write_some and send ? read_somereceive有什么区别, write_somesend有什么区别? Thanks!谢谢!

As far as I remember, read_some and receive are actually doing the same.据我记得, read_somereceive实际上是在做同样的事情。 I think receive just calls read_some or vice versa.我认为只接收调用 read_some 或反之亦然。 The one naming comes from the idea of treating a socket as a file (read/write), while the other one rather comes from a connection (send /receive) point of view.一个命名来自将套接字视为文件(读/写)的想法,而另一个命名来自连接(发送/接收)的观点。 Same should be true for write_some and send . write_somesend也应该如此。

In BOOST ASIO documentation , section TCP Clients says:BOOST ASIO 文档中, TCP 客户端部分说:

Data may be read from or written to a connected TCP socket using the receive(), async_receive(), send() or async_send() member functions.可以使用 receive()、async_receive()、send() 或 async_send() 成员函数从连接的 TCP 套接字读取或写入数据。 However, as these could result in short writes or reads , an application will typically use the following operations instead: read(), async_read(), write() and async_write().但是,由于这些可能导致写入或读取时间较短,因此应用程序通常会使用以下操作:read()、async_read()、write() 和 async_write()。

the same.相同。 both call this->get_service().send()都调用 this->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;
}

from basic_stream_socket.hpp来自 basic_stream_socket.hpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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