简体   繁体   中英

Get UDP datagram length via Boost Asio?

Is it possible to get the length of the UDP datagram via Boost Asio? UDP headers have a field that specifies the length of the UDP packet. Is it possible to get this value via Boost Asio? If not, how does one determine the length of the packet?

asio allows you to bind completion handlers when executing reads. One of the arguments of the completion handler is the packet length.

Your completion handler for a read must meet the requirements specified in http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/ReadHandler.html

For example look at http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/echo/async_udp_echo_server.cpp . In the call to async_receive_from you specify which completion handler is to be called once the read completes:

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

Thus when the completion handler handle_receive_from is executed, asio passes the size of the bytes read to the handler.

void handle_receive_from(const boost::system::error_code& error,
  size_t bytes_recvd)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM