开发者

Difference between read_some/write_some and receive/send?

开发者 https://www.devze.com 2023-03-27 23:12 出处:网络
I am beginning to work with Boost Asio\'s TCP sockets.W开发者_如何学编程hat is the difference between read_some and receive and what is the difference between write_some and send?Thanks!As far as I re

I am beginning to work with Boost Asio's TCP sockets. W开发者_如何学编程hat is the difference between read_some and receive and what is the difference between write_some and send? Thanks!


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.


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().


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号