C++ 读/写与接收/发送之间的区别?
我开始使用Boost Asio的TCP套接字。和的区别是什么和的区别是什么?谢谢 据我记忆所及,读一些和接收实际上也在做同样的事情。我认为只接电话,读一些,反之亦然。一个命名源于将套接字视为文件(读/写),而另一个命名源于连接(发送/接收)的观点。同样的情况也适用于写一些和发送同样的内容。两者都调用此->获取服务().send()C++ 读/写与接收/发送之间的区别?,c++,boost,boost-asio,C++,Boost,Boost Asio,我开始使用Boost Asio的TCP套接字。和的区别是什么和的区别是什么?谢谢 据我记忆所及,读一些和接收实际上也在做同样的事情。我认为只接电话,读一些,反之亦然。一个命名源于将套接字视为文件(读/写),而另一个命名源于连接(发送/接收)的观点。同样的情况也适用于写一些和发送同样的内容。两者都调用此->获取服务().send() ///在套接字上发送一些数据。 /** *此函数用于在流套接字上发送数据。功能 *调用将阻塞,直到发送一个或多个字节的数据 *成功,或直到发生错误。 * *@para
///在套接字上发送一些数据。
/**
*此函数用于在流套接字上发送数据。功能
*调用将阻塞,直到发送一个或多个字节的数据
*成功,或直到发生错误。
*
*@param缓冲要在套接字上发送的一个或多个数据缓冲区。
*
*@返回发送的字节数。
*
*@throws boost::system::失败时引发系统_错误。
*
*@注意发送操作可能不会将所有数据发送给对等方。
*如果需要确保所有数据,请考虑使用@ REF写入函数
*在阻塞操作完成之前写入。
*
*@par示例
*要发送单个数据缓冲区,请使用@ref buffer函数,如下所示:
*@code
*send(boost::asio::buffer(数据、大小));
*@endcode
*有关发送多个缓冲区的信息,请参阅@ref缓冲区文档
*一次性缓冲区,以及如何与数组一起使用,boost::array或
*向量。
*/
模板
std::size\u t发送(const ConstBufferSequence&buffers)
{
boost::system::error_code ec;
std::size\u t s=this->get\u service().发送(
这->获取实现(),缓冲区,0,ec);
boost::asio::detail::抛出错误(ec,“发送”);
返回s;
}
////////////////////////////////////////////
模板
std::size\u t write\u some(const ConstBufferSequence&buffers)
{
boost::system::error_code ec;
std::size\u t s=this->get\u service().发送(
这->获取实现(),缓冲区,0,ec);
boost::asio::detail::throw_error(ec,“write_some”);
返回s;
}
从中的basic_stream_socket.hpp中,TCP客户机部分显示:
可以使用从连接的TCP套接字读取或写入数据
receive()、async_receive()、send()或async_send()成员函数。
然而,由于这些可能导致,
应用程序通常会使用以下操作:
read()、async_read()、write()和async_write()
你有一些参考/链接吗?我在boost文档中找不到任何示例,boost示例也没有使用
send
/receive
。
/// 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;
}