如何在使用boost asio时获得UDP远程端点

How to I get the UDP remote endpoint when using boost asio?

本文关键字:程端点 UDP 端点 boost asio      更新时间:2023-10-16

假设我已经重新编码了UDP套接字(加入多播组)上的异步接收处理程序:

         udpSocket.async_receive(boost::asio::buffer(buffer, sizeof(buffer)),
                                 boost::bind(&receiveCallback,
                                              boost::asio::placeholders::error,
                                              boost::asio::placeholders::bytes_transferred));

这里的想法是,我想在接收UDP数据包时获得远程地址和端口在receiveCallBack。怎么做呢?是否有特定的占位符?如果不是,在回调中如何获得该信息?

我想你可能想使用receive_from调用,它接受boost::asio::ip::udp::endpoint来捕获发送方数据。

在这里的boost asio示例中有一个示例多播接收器。特别是这个块:

socket_.async_receive_from(
    boost::asio::buffer(data_, max_length), sender_endpoint_,
    boost::bind(&receiver::handle_receive_from, this,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));