How to read UDP packet with variable length in C

后端 未结 4 1392
天命终不由人
天命终不由人 2021-01-13 15:34

I\'m sending a C struct over UDP

struct packet{
    int numInt;
    int* intList; //malloc\'ed as (sizeof(int)*numInt)
}

It will be seriali

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 15:47

    You could try using a small buffer, just large enough to get numInt, with the MSG_PEEK flag set. Then you can find out the size you actually need, and receive again without MSG_PEEK to get the whole thing.

提交回复
热议问题