簡體   English   中英

read_some/write_some 和接收/發送之間的區別?

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

我開始使用 Boost Asio 的 TCP sockets。 read_somereceive有什么區別, write_somesend有什么區別? 謝謝!

據我記得, read_somereceive實際上是在做同樣的事情。 我認為只接收調用 read_some 或反之亦然。 一個命名來自將套接字視為文件(讀/寫)的想法,而另一個命名來自連接(發送/接收)的觀點。 write_somesend也應該如此。

BOOST ASIO 文檔中, TCP 客戶端部分說:

可以使用 receive()、async_receive()、send() 或 async_send() 成員函數從連接的 TCP 套接字讀取或寫入數據。 但是,由於這些可能導致寫入或讀取時間較短,因此應用程序通常會使用以下操作:read()、async_read()、write() 和 async_write()。

相同。 都調用 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM