Analyzing Why UDP Broadcasts Are Not Received

Due to business requirements, UDP broadcast and message reception were needed. However, testing revealed that UDP broadcast messages could be received using the nc command but not from within the program. This post documents the analysis process.

Root Cause

Searching on Stack Overflow, I found a similar question: Python UDP Broadcast not sending, where Adam Rosenfield noted:

connect is fine with UDP, though it’s not standard practice. All it does is set the default destination address for all future send calls, and it limits the allowed source addresses from all future recv calls on the socket. It doesn’t actually do any sort of network connection.

Reviewing my code, I found that connect had indeed been called, which allowed sending to work normally but prevented reception of broadcast messages.

In general, for UDP broadcast you should call sendto directly with the broadcast address, and make sure to set the SO_BROADCAST socket option.

References