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;89 /* 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 );1314 /* Check the socket was created. */15 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );1617 /* 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 ) );2223 for( ;; )24 {25 /* Receive data from the socket. ulFlags is zero, so the standard26 interface is used. By default the block time is portMAX_DELAY, but it27 can be changed using FreeRTOS_setsockopt(). */28 lBytes = FreeRTOS_recvfrom( xListeningSocket,29 cReceivedString,30 sizeof( cReceivedString ),31 0,32 &xClient,33 &xClientLength );3435 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;89 /* 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 );1314 /* Check the socket was created. */15 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );1617 /* 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 ) );2223 for( ;; )24 {25 /* Receive data from the socket. ulFlags is zero, so the standard26 interface is used. By default the block time is portMAX_DELAY, but it27 can be changed using FreeRTOS_setsockopt(). */28 lBytes = FreeRTOS_recvfrom( xListeningSocket,29 cReceivedString,30 sizeof( cReceivedString ),31 0,32 &xClient,33 &xClientLength );3435 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