UDP broadcast

alainm3 wrote on Monday, October 30, 2017:

Hi, a colleague is using FreeRTOS+TCP…
He is abandoning it saying that it is not possible to make an UDP broadcast !!!

Could you please give me a clue of where I can find that specific command?

Thanks, Alain

davedoors wrote on Monday, October 30, 2017:

FreeRTOS+TCP uses LLMR which uses a UDP broadcast. See how that is implemented.

heinbali01 wrote on Tuesday, October 31, 2017:

Indeed, UDP broadcasts can be sent and received using the FreeRTOS+TCP library.

Here is an example of sending a UDP broadcast:

BaseType_t xSendBroadcast( Socket_t xSocket, uint32_t ulIPAddress, uint16_t usPort, uint8_t *pucMessage, size_t uxLength )
BaseType_t xResult;
struct freertos_sockaddr xAddress;

	memest( &xAddress, '\0', sizeof( xAddress ) );
    xAddress.sin_addr = FreeRTOS_htonl( ulIPAddress );
    xAddress.sin_port = FreeRTOS_htons( usPort );

    xResult = FreeRTOS_sendto( xSocket, ( void * )pucMessage, uxLength, 0, &xAddress, sizeof( xAddress ) );

    return xResult;

Some remarks about UDP broadcasts:

  1. Usually a broadcast won’t be routed towards neighbouring networks. I have a LAN here with switches and a router. The router does pass broadcasts to all local (192.168.1.x) segments, but it will not forward them to the Internet. Which is logical.

  2. In some companies, the network administrator may also limit the usage of UDP broadcasts, in order to decrease network usage, or in order to increase security. Frequent Broadcast packets can be very disturbing.

The earlier an unsolicited packet can be filtered-out, the less it will disturb. I have seen FreeRTOS+TCP devices in factories that sometime became unreachable because of thousands of unwanted broadcasts. I solved this by setting:


and do the filtering at the earliest possible moment in the driver.

  1. When sending a broadcast, the library will use a special MAC address, existing of all one’s. The IP-address must be chosen by you. You can choose either of these:
    /* The generic broadcast IP address: */
    uint32_t ulIPAddress = FreeRTOS_inet_addr( "" );

    /* A network-specific broadcast IP address, which is preferred: */
    uint32_t ulIPAddress = FreeRTOS_inet_addr( "" );
    uint32_t ulIPAddress = FreeRTOS_inet_addr( "" );

    /* In a generic way: */
    uint32_t ulIPAddress = ( ulMyIPAddress & ulNetMask ) |  ~ulNetMask;
  1. UDP broadcasts do not traverse the Internet, but normal UDP packets do! Think of the Network Time Protocol (NTP).

  2. Beside UDP broadcasts, multi-casting is often used. It is kind of replacing the broadcasting. What is the difference?

A broadcast is unspecified. The packet wants to be spread out to every node in the network. The MAC address is ff:ff:ff:ff:ff:ff and the IP address may indicate the desired network ( as in ).

Multicast packets look very much like broadcast packets, except that they use special MAC- and IP-addresses. Ethernet adapters can use hardware filters to stop multi-cast packets if they like. Multicasting is to be preferred above broadcasting.

Dave wrote:

FreeRTOS+TCP uses LLMR which uses a UDP broadcast. See how that is implemented.

LLMNR is actually a multi-cast protocol. It uses the MAC address 01:00:5e:00:00:fc and the IP address

If anyone would like to use multi-casting with FreeRTOS+TCP, please say so and I will think of making some extensions.


alainm3 wrote on Tuesday, October 31, 2017:

Thanks guys for such a good explanation :slight_smile: :slight_smile: