Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Jun 2025

Receiving UDP Data (standard interface)

Part of the FreeRTOS-Plus-TCP Networking Tutorial

The FreeRTOS_recvfrom() TCP/IP stack API function is used to receive from a UDP socket. Data can only be received after the socket has been created, configured, and bound to a local port number.

As detailed on the FreeRTOS_recvfrom() API reference page, FreeRTOS_recvfrom() can be used with standard calling semantics, or zero copy calling semantics. This page demonstrates the standard calling semantics.

The source code below shows a RTOS task that creates a socket before entering a loop that receives data using the standard (as opposed to zero copy) calling semantics. Both IPv4 and IPv6 use cases are shown. Bothe IPv4 and IPv6 use cases are shown.

IPv4

1static void vUDPReceivingUsingStandardInterface( void *pvParameters )
2{
3long lBytes;
4uint8_t cReceivedString[ 60 ];
5struct freertos_sockaddr xClient, xBindAddress;
6uint32_t xClientLength = sizeof( xClient );
7Socket_t xListeningSocket;
8
9 /* Attempt to open the socket. */
10 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET,
11 FREERTOS_SOCK_DGRAM, /* FREERTOS_SOCK_DGRAM for UDP */
12 FREERTOS_IPPROTO_UDP );
13
14 /* Check the socket was created. */
15 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
16
17 /* Bind to port 10000. */
18 memset( &xBindAddress, 0, sizeof(xBindAddress) );
19 xBindAddress.sin_port = FreeRTOS_htons( 10000 );
20 xBindAddress.sin_family = FREERTOS_AF_INET4;
21 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
22
23 for( ;; )
24 {
25 /* Receive data from the socket. ulFlags is zero, so the standard
26 interface is used. By default the block time is portMAX_DELAY, but it
27 can be changed using FreeRTOS_setsockopt(). */
28 lBytes = FreeRTOS_recvfrom( xListeningSocket,
29 cReceivedString,
30 sizeof( cReceivedString ),
31 0,
32 &xClient,
33 &xClientLength );
34
35 if( lBytes > 0 )
36 {
37 /* Data was received and can be process here. */
38 }
39 }
40}
41

Example using FreeRTOS_recvfrom() with the standard (as opposed to zero copy) calling semantics

IPv6

1static void vUDPReceivingUsingStandardInterface( void *pvParameters )
2{
3 long lBytes;
4 uint8_t cReceivedString[ 60 ];
5 struct freertos_sockaddr xClient, xBindAddress;
6 uint32_t xClientLength = sizeof( xClient );
7 Socket_t xListeningSocket;
8
9 /* Attempt to open the socket. */
10 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET6, /* FREERTOS_AF_INET6 for IPv6 socket */
11 FREERTOS_SOCK_DGRAM, /*FREERTOS_SOCK_DGRAM for UDP.*/
12 FREERTOS_IPPROTO_UDP );
13
14 /* Check the socket was created. */
15 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
16
17 /* Bind to port 10000. */
18 memset( &xBindAddress, 0, sizeof(xBindAddress) );
19 xBindAddress.sin_port = FreeRTOS_htons( 10000 );
20 xBindAddress.sin_family = FREERTOS_AF_INET6;
21 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
22
23 for( ;; )
24 {
25 /* Receive data from the socket. ulFlags is zero, so the standard
26 interface is used. By default the block time is portMAX_DELAY, but it
27 can be changed using FreeRTOS_setsockopt(). */
28 lBytes = FreeRTOS_recvfrom( xListeningSocket,
29 cReceivedString,
30 sizeof( cReceivedString ),
31 0,
32 &xClient,
33 &xClientLength );
34
35 if( lBytes > 0 )
36 {
37 /* Data was received and can be process here. */
38 }
39 }
40}

Example using FreeRTOS_recvfrom() with the standard (as opposed to zero copy) calling semantics

Back to the RTOS TCP networking tutorial index