开发者

boost::asio udp - How do I get many mutable buffers filled?

I'm trying to receive many udp messages from one async_receive call. My messages are approx. 60 bytes long.

I'm giving an async_receive call a开发者_运维百科 buffer array very similar to the boost docs but can't seem to get all the buffers filled.

char d1[128];
char d2[128];
char d3[128];

boost::array<boost::asio::mutable_buffer, 3> bufs = 
{
   boost::asio::buffer(d1),
   boost::asio::buffer(d2),
   boost::asio::buffer(d3) 
};

_socket.async_receive(bufs, handler);

When my handler gets called the bytes_transferred is equal to one message length (i.e. 60).

Any thoughts on how I can get the second and third buffer populated? Also, how do I now if the second and third mutable buffer were populated?


If you want to receive multiple datagrams in a single call, you generally (regardless of Boost) need to use recvmmsg. From what I can tell, Boost does not use recvmmsg, so you would need to use it yourself with the native socket held by Boost ASIO. The advantage of doing this is that you can reduce system calls when multiple datagrams are available.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜