0

When using UDP sockets, is it guaranteed that when A calls send() once, that, provided the buffer is big enough and the packet gets successfully transmitted (without any bit-flips or data loss), the whole packet will be read on the other side by B with also exactly one recv() call?

cooky451
  • 3,460
  • 1
  • 21
  • 39
  • possible duplicate of [Is a UDP packet guaranteed to be complete, practical sense, if delivered?](http://stackoverflow.com/questions/15954813/is-a-udp-packet-guaranteed-to-be-complete-practical-sense-if-delivered) – Mats Petersson Jul 09 '13 at 15:29
  • I'm pretty certain the result of UDP packet transmission is "not guaranteed at all" - in other words, your application will need to check everything from size to order and correctness. – Mats Petersson Jul 09 '13 at 15:30
  • @MatsPetersson Thanks, but that's not the same question. Im well aware of the fact that the packet doesn't need to be "correct", I just want to know if it's guaranteed to come in one "batch". – cooky451 Jul 09 '13 at 15:31
  • @MatsPetersson Not size. UDP datagrams arrive intact or not at all. – user207421 Jul 10 '13 at 09:45
  • So... apparently this is off topic... okay... – cooky451 Jul 10 '13 at 15:54

1 Answers1

3

Yes. Either a complete datagram arrives or nothing.

Note that reading only part of it will still throw away the rest.

EDIT:
More elaborately, when you call send on an UDP socket, you send out exactly one datagram (which may be fragmented to several packets and reassembled on underlying protocols such as IP, ethernet, or ATM). This datagram either arrives and passes the checksum, or it doesn't.
If everything went well, you have one complete datagram queued in your receive buffer, which you can recv on exactly once.

Calling recv will copy data from the oldest datagram in the receive buffer up to the limit that you specify (len parameter) into your application buffer, and then discards the datagram, even if not all of it has been read. The next call to recv will read a different datagram (the next oldest).

Thus, you really have a strict 1:1 relationship.

Damon
  • 67,688
  • 20
  • 135
  • 185