reading partially from sockets
sockets, udp
Solution
Each read from UDP socket de-queues one whole datagram off kernel socket receive buffer no matter what's your userland buffer size. That is:
- If your buffer is bigger then the next pending datagram, you'll read less then your buffer size.
- If your buffer is smaller, you'll read your buffer size worth and the rest of the data is discarded.
- You can set `MSG_TRUNC` option in the `flags`, so `recv(2)` will return the whole datagram length, not just the part you read into your userland buffer.
Hope this helps.
Problem
I'm having a little test program that sends a lot of udp packets between client->server->client (ping/pong test). The packets are fixed size on each run (last run is max allowable size of udp packet) I'm filling the packets with random data except for the beginning of each packet that contains the packet number. So I'm only interested to see if I receive all the packets back at the client. I'm using `sendto()` and `recvfrom()` and I only read the sizeof(packet_number) (which in this case is an int). What happens to the rest of the data? Does it end up in fairyland (gets discarded)? Or does the new packet that arrives gets appended to this "old" data? (using linux)