C++, receive SOCK_DGRAM over UDP IPC with unknown size

49 views Asked by At

I need to be able to listen on IPC UDP socket and receive messages with unknown size (in C++). Those messages are serialized capnp with some dynamic arrays.

recv(socket_fd, buff, buff_len, 0)

How do I size my receive buffer properly?

Can I check what is the message size of the next packet in the queue and resize buffer accordingly? Is it much overhead to check the size (ms or us)?

For reference, I expect messages to be up to 500Kb.

1

There are 1 answers

6
Steffen Ullrich On

For reference, I expect messages to be up to 500Kb.

The maximum datagram payload size in UDP is 65507 bytes. If your application protocol has larger messages then it needs some way to split these messages into multiple UDP datagrams and handle packet loss, duplication and reordering when reconstructing the message.

Can I check what is the message size of the next packet in the queue and resize buffer accordingly?

You can simply use a buffer of 64k since the datagrams cannot be larger.