本文整理汇总了C++中boost::asio::ip::udp::socket::receive_from方法的典型用法代码示例。如果您正苦于以下问题:C++ socket::receive_from方法的具体用法?C++ socket::receive_from怎么用?C++ socket::receive_from使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::asio::ip::udp::socket
的用法示例。
在下文中一共展示了socket::receive_from方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: echo
void session::echo(std::vector<uint8_t> const& request)
{
auto reply = request;
socket_.send_to(boost::asio::buffer(request), endpoint_);
std::size_t reply_length = socket_.receive_from(boost::asio::buffer(reply), endpoint_);
if (reply_length != request.size())
throw std::logic_error("Different size of reply and request");
if (reply != request)
throw std::logic_error("Different data value of request and reply");
}
示例2: receive
void Server::receive() {
Peer peer;
boost::system::error_code error;
auto bytes_transferred =
socket_.receive_from(boost::asio::buffer(recv_buffer_), peer, 0, error);
std::cout << "Server received " << bytes_transferred << " bytes"
" from " << peer
<< " with " << error.message() << std::endl;
if (!error) {
std::cout << " >> ";
std::cout.write(recv_buffer_.data(), bytes_transferred);
std::cout << std::endl;
}
receive();
}