2

A lot of examples can be found about non blocking TCP sockets, however I find it difficult to find good explanation about how UDP sockets should be handled with poll/select/epoll system calls.

  • Blocking or non blocking ?

When we have to deal with tcp sockets then it makes sense to set them to non blocking, since it takes only one slow client connection to prevent the server from serving other clients. However, there are no ACK messages in UDP, so my assumption is that writing to UDP should be fast enough for both cases. Does that mean that we can safely use blocking UDP socket with the family of poll system calls if each time we are going to send small amount of data (10Kb for example)? From this discussion I assume that ARP request is the only point that can substantially block the sendto function, however isn't it a one time thing?

  • Return value of sendto

Let's say the socket is non-blocking. Can there be a scenario that I try to send 1000 bytes of data, and the sendto function sends only some part of it (say 300 bytes)? Does that mean that it has just sent a UDP packet with 300 bytes, and next time I use sendto I have to consider that it will send in a new UDP packet again? Is this situation still possible for blocking sockets?

  • Return value of recvfrom

The same question applies for recvfrom. Can there be a situation that I will need to call recvfrom more than once to obtain the full UDP packet. Is that behaviour different for blocking/non-blocking sockets.

samvel1024
  • 1,123
  • 4
  • 15
  • 39
  • 2
    UDP is message oriented, not stream oriented. In UDP, there is a 1:1 relationship between sends and reads, unlike in TCP. What you give to `sendto` is sent as-is in a single message, there are no partial messages. If the data can't be sent in a single message, the send fails. `recvfrom` reads whole messages only. If your buffer is too small to receive a message's whole data, the data is truncated and the remainder is lost. – Remy Lebeau Jun 01 '19 at 06:06
  • @RemyLebeau is that behaviour same regardless if the socket is blocking or not ? – samvel1024 Jun 01 '19 at 10:18
  • 1
    yes, the behavior is the same. There are no partial sends/reads in UDP – Remy Lebeau Jun 01 '19 at 14:26

0 Answers0