Reputation: 993
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?
Upvotes: 1
Views: 2293
Reputation: 9573
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)
Upvotes: 2