2
votes

Confused about a concept...

Suppose if the client call sendto() twice, each time sending datagram of 100 bytes to the server. Then if the server call recvfrom() with a receive buffer of 200 bytes, will it be receiving both datagrams into the same buffer?

From what I learn.. if i do sendto(100) , then I will be receiving back the recvfrom(100) back on the same datagram transport "space"

but since it is sent twice of 100..will the recvfrom(200) be on a different buffer then?

Thanks.

3

3 Answers

4
votes

Because you are using UDP, if you call sendto 2 times, you will have 2 recvfrom as well. Preserving the content of the packet completely. You will never receive 200 bytes if you send 2 times 100 bytes.

I must warn you that UDP is best effort. It is possible that the packet is dropped and that you receive only one packet, the first or the second, or even no packet at all.

Another warning, you have to make sure that the receive buffer is large enough, in your description this is ok, but if the receive buffer is too small, some implementations drop the packet, some chop it off.

UDP is actually a very thin layer on top of IP. IP is the one who will deliver the packets from one end to the other. From a user perspective the packets that are being sent are untouched.

2
votes

For UDP one recv(2) consumes exactly one input datagram. Larger SO_RCVBUF values allow for buffering more datagrams in the kernel, but don't not "glue" them together.

If you are on Linux, it provides a non-portable recvmmsg(2), which can read multiple datagrams in one system call.

1
votes

Depends on the protocol. For a stream protocol such as TCP there are no message boundaries and you can perceive any chunking at all. For a message based protocol such as UDP messages are preserved and you'll read 100 bytes twice (assuming the packets were not lost).