Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect receipt of a 0-length UDP datagram

Tags:

c

sockets

udp

I was considering writing/implementing a UDP-based protocol that would use a zero-length datagram as a 'hello' message. And while I don't expect to have a problem sending a zero-length datagram, I'm not certain I can receive one.

recvfrom returns the number of bytes read, but 0 is reserved for an orderly shutdown.

read returns number of bytes read, but 0 is reserved for EOF.

select "will be watched to see if characters become available for reading".

How would one detect the receipt of a zero-length datagram?

like image 734
Jumbogram Avatar asked Mar 15 '11 02:03

Jumbogram


1 Answers

When calling recvfrom on a TCP Socket, you will receive a zero byte read if a FIN packet has been received (an orderly shutdown). UDP has no concept of orderly shutdowns, and no data is transmitted from the sender to receiver to indicate a socket being closed. The protocol is completely stateless and every datagram received is independent from the receiver's point of view. As such, I am not aware of any scenerios in which a zero byte return code from recvfrom on a UDP socket would be caused by anything other than a zero length datagram being received.

like image 151
bdk Avatar answered Sep 25 '22 09:09

bdk