16

I'm just cleaning up some code we wrote a while back and noticed that for a udp socket, 0 is being treated as the connection closed.

I'm quite sure this was the result of porting the same recv loop from the equivalent tcp version. But it makes me wonder. Can recv return 0 for udp? on tcp it signals the other end has closed the connection. udp doesn't have the concept of a connection so can it return 0? and if it can, what is it's meaning?

Note: the man page in linux does not distinguish udp and tcp for a return code of zero which may be why we kept the check in the code.

2
  • Are the UDP sockets connected or not? Sep 20, 2012 at 4:02
  • You can call "connect" on UDP sockets, but all that does is assign a remote IP address/port to the UDP socket. That allows you to call just "send" and "recv" without sendto, recvfrom. I believe it also acts as a filter and only allows you to receive udp datagrams only from that address as well. So in a sense they are connected, but not like with TCP.
    – hookenz
    Sep 20, 2012 at 21:05

2 Answers 2

18

udp doesn't have the concept of a connection so can it return 0? and if it can, what is it's meaning

It means a 0-length datagram was received. From the great UNP:

Writing a datagram of length 0 is acceptable. In the case of UDP, this results in an IP datagram containing an IP header (normally 20 bytes for IPv4 and 40 bytes for IPv6), an 8-byte UDP header, and no data. This also means that a return value of 0 from recvfrom is acceptable for a datagram protocol: It does not mean that the peer has closed the connection, as does a return value of 0 from read on a TCP socket. Since UDP is connectionless, there is no such thing as closing a UDP connection.

7
  • Cannot remember is a datagram guaranteed to be delivered or not unlike tcp where you can receive some of the data. Sep 20, 2012 at 4:12
  • @AdrianCornish I think all modern stacks try to deliver it even if there's no payload.
    – cnicutar
    Sep 20, 2012 at 4:14
  • Phrased that question badly - for example I could send 100 bytes over a tcp connection and the other end may only get 50 and need to read again - so I suppose the real question is can a datagram be fragmented at the application layer Sep 20, 2012 at 4:20
  • 1
    @AdrianCornish No, it can't. One send always means one receive under UDP. Even if IP fragments it (not the application layer but rather the network layer) it will still be reassembled and one recv will be triggered.
    – cnicutar
    Sep 20, 2012 at 4:23
  • 2
    @cnicutar "One send always means one receive under UDP" is badly phrased. The data sent by send() is received either intact and whole or not at all in UDP. However packet loss means there is no 1::1 correspondence between sends and receives.
    – user207421
    Sep 20, 2012 at 10:22
-2

In Linux there are two reasons that recvfrom on a UDP socket could return zero:

1) a zero-length datagram was received, or 2) shutdown was called on the socket

The second behavior is useful because it allows you to unblock a thread that is waiting on the socket. However, there is no way for the caller of recvfrom to know whether the socket was shut down or whether a zero-length datagram was received. For this you need some other way to signal to the thread that the socket was shut down, for example using a shared variable.

The second behavior also seems to contradict the recv(2) man page, which says this:

When  a  stream socket peer has performed an orderly shutdown, the return 
value will be 0 (the traditional "end-of-file" return).

Clearly, it also occurs for UDP sockets, which are not stream sockets.

1
  • Stream sockets differ from UDP sockets in their behaviour. So there is no condradiction in the man page. The is no need to call shutdown on a UDP socket. Only point 1 us correct. Your answer does not add anything new that has already been answered clearly by the accepted answer
    – hookenz
    Jan 4, 2020 at 1:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.