Tôi bắt đầu làm việc với ổ cắm TCP của Boost Asio. Sự khác nhau giữa read_some
và receive
và sự khác nhau giữa write_some
và send
là gì? Cảm ơn!Sự khác biệt giữa read_some/write_some và nhận/gửi?
Trả lời
Theo tôi nhớ, read_some và nhận được thực sự đang thực hiện tương tự. Tôi nghĩ chỉ nhận được cuộc gọi read_some hoặc ngược lại. Việc đặt tên đến từ ý tưởng xử lý một ổ cắm như một tập tin (đọc/ghi), trong khi một tên khác xuất phát từ một quan điểm kết nối (gửi/nhận). Điều này cũng đúng cho write_some và gửi.
giống nhau. . Cả hai gọi this-> get_service() gửi()
/// 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;
}
từ basic_stream_socket.hpp
Trong BOOST ASIO documentation, phần TCP Clients nói:
dữ liệu có thể được đọc từ hay viết vào ổ cắm TCP được kết nối bằng cách sử dụng các hàm thành viên nhận được(), async_receive(), send() hoặc async_send() thành viên . Tuy nhiên, vì chúng có thể dẫn đến short writes or reads, một ứng dụng thường sẽ sử dụng các hoạt động sau đây thay thế: đọc(), async_read(), write() và async_write().
Bạn có một số tham khảo/liên kết không? Tôi không thể tìm thấy bất kỳ tài liệu nào trong tài liệu tăng cường và các ví dụ tăng không sử dụng 'send' /' receive'. – overcoder