Reputation: 1769
I'm using the boost::asio::ip::udp::socket to receive UDP packets via socket's async_receive_from method.
The code works fine, the only problem is that in the time I process a packet, lots more come creating a queue (the buffer) to process. My program though must sink all the packets received since the start of the processing, so that it listens only to the most recent ones.
Example:
Is there any way to discard the packets in the middle? Thanks!
Upvotes: 0
Views: 1830
Reputation: 7357
I think it can be done quite simple:
async_receive_from
until got a packet.available
method to determine is more data in the socket.Upvotes: 0
Reputation: 182761
Use a buffer that holds only a single datagram.
Keep reading into the buffer until there are no more datagrams to read.
If you read at least one packet, process the datagram in the buffer.
Go to step 2.
Note that UDP is a datagram protocol, not a packet protocol. A single UDP datagram can be split over multiple packets.
Upvotes: 1