1

The return value of Socket.SendTo is an int representing the number of bytes that were actually sent from the buffer. Does this imply that a partial datagram can be sent, one containing only some of the data that was requested to be sent?

If so, does the programmer need to do any work to reassemble the datagrams when they're received, or is it handled transparently?


This old question's been overhauled for clarity so as to be more helpful to future searchers.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • 'Assuming all the data wasn't sent' - well, did you check it? You can't send anything less than a whole datagram, (except no datagram). You can, however, have less than a whole datagram copied into your rx buffer if it's not big enough. – Martin James Jan 03 '15 at 21:18
  • If the SendTo does not return an error, the datagram in its entirety should get sent. If it does return an error, nothing will get sent. There is no question of a partial datagram getting sent - it won't happen. If you have some secure protocol on top of UDP that can identify missing datagrams at the peer and notify back the omission, you will always have to resend the entire datagram. – Martin James Jan 03 '15 at 21:52
  • @MartinJames Can you give some reference? I am new to socket programming. I am told so too. Thanks – Rick May 14 '20 at 14:53

1 Answers1

1

UDP is a message-oriented protocol, so it's guaranteed that complete datagrams are sent, not partial datagrams. It is however possible that datagrams are simply too long to be sent, which is an error exposed to the programmer on at least Linux and Windows.

After UDP does its processing, we then have two branches for the underlying protocol:

  • IPV4

    The IP protocol guarantees that packets that arrive are error-free, that is "whole" (source):

    IPv4 provides safeguards to ensure that the IP packet header is error-free. A routing node calculates a checksum for a packet. If the checksum is bad, the routing node discards the packet.

    The protocol layers below UDP (IP, Link etc) may split up the data (fragmentation), but they'll guarantee that when it's yielded to the UDP layer the packet is the same as the one that entered their protocol layer on the sender.

  • IPV6

    The IP protocol no longer guarantees packets arrive error-free, but the UDP pseudo-headers used over IPv6 include checksum information so rather than the IP layer guaranteeing this, the UDP layer does.

As such, datagrams are guaranteed to be sent whole, and (if they're received) to be error-free. This is summed up by the following from this comparison between UDP and TCP:

Packets have definite boundaries which are honored upon receipt, meaning a read operation at the receiver socket will yield an entire message as it was originally sent.

hnefatl
  • 5,860
  • 2
  • 27
  • 49