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

发布于 2024-11-29 10:58:56 字数 604 浏览 4 评论 0原文

我开始使用 Boost Asio 的 TCP 套接字。 read_some< 之间有什么区别/a> 和 接收是什么write_some发送?谢谢!

I am beginning to work with Boost Asio's TCP sockets. What is the difference between read_some and receive and what is the difference between write_some and send? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白况 2024-12-06 10:58:56

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

As far as I remember, read_some and receive are actually doing the same. I think receive just calls read_some or vice versa. 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.

流年里的时光 2024-12-06 10:58:56

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

可以使用以下命令从连接的 TCP 套接字读取数据或将数据写入连接的 TCP 套接字
receive()、async_receive()、send() 或 async_send() 成员函数。
但是,因为这些可能会导致 短写入或者
读取

应用程序通常会使用以下操作:
read()、async_read()、write() 和 async_write()。

In BOOST ASIO documentation, section TCP Clients says:

Data may be read from or written to a connected TCP socket using the
receive(), async_receive(), send() or async_send() member functions.
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().

走过海棠暮 2024-12-06 10:58:56

相同。 调用 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;
}

两者都从 basic_stream_socket.hpp

the same. both call 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文