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.
However, you can actually use connect() on UDP socket as an option. In that case, you can use send()/recv() on the UDP socket to send data to the address specified with the connect() and to receive data only from the address.
By default, Linux UDP does path MTU (Maximum Transmission Unit) discovery. This means the kernel will keep track of the MTU to a specific target IP address and return EMSGSIZE when a UDP packet write exceeds it. When this happens, the application should decrease the packet size.
The UDP Send block transmits an input data vector as a UDP packet to a remote IP network port. The remote IP port number to which the block sends the UDP packets is tunable in the generated code.
Close the socket Since there is no concept of a connection in UDP, there is no need to call shutdown.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With