Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under Linux, can recv ever return 0 on UDP?

Tags:

c

linux

udp

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.

like image 696
hookenz Avatar asked Sep 20 '12 04:09

hookenz


People also ask

Does UDP use recv?

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.

Does Linux use UDP?

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.

Can UDP Send block?

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.

Do UDP sockets need to be closed?

Close the socket Since there is no concept of a connection in UDP, there is no need to call shutdown.


1 Answers

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.

like image 197
cnicutar Avatar answered Sep 19 '22 08:09

cnicutar