I’m sending a C struct over UDP
struct packet{
int numInt;
int* intList; //malloc'ed as (sizeof(int)*numInt)
}
It will be serialized as [numInt][intList[0]]...[intList[numInt-1]].
My understanding is that calling recvfrom on UDP will read the entire packet, even if the buffer doesn’t hold that many bytes. Is using a really large buffer the only option I have?
You could pass
MSG_PEEKtorecvfromto find out exactly how big the buffer needs to be. So justrecvfroma few bytes withMSG_PEEKto findnumIntand thenrecvfromthe real thing (this time withoutMSG_PEEK).The standard says something about
MSG_PEEK, but kernel.org spells it better:Obviously at some point you will start wondering if doubling the number of system calls to save memory is worth it. I think it isn’t.