Download FreeRTOS

Quality RTOS & Embedded Software

FreeRTOS website now available in Simplified Chinese
New FreeRTOS Long Term Support version now available.
FreeRTOS Extended Maintenance Program (EMP) registration now open.
FreeRTOS-Plus-TCP v3.0.0 released:
Featured FreeRTOS IoT Integrations:

Create, Configure and Bind a UDP Socket
Part of the FreeRTOS-Plus-TCP Networking Tutorial

UDP Sockets are created using the FreeRTOS_socket() API function with the xType (second) parameter set to FREERTOS_SOCK_DGRAM and the xProtocol (third) parameter set to FREERTOS_IPPROTO_UDP. They are configured using the FreeRTOS_setsockopt() function, and bound to a port (if necessary) using the FreeRTOS_bind() function.

static void prvSimpleUDPServerTask( void *pvParameters )
long lBytes;
struct freertos_sockaddr xBindAddress;
Socket_t xListeningSocket;
const TickType_t xSendTimeOut = 200 / portTICK_PERIOD_MS;

/* Attempt to open the UDP socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET,

/* Check for errors. */
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );

/* Ensure calls to FreeRTOS_sendto() timeout if a network buffer cannot be
obtained within 200ms. */

FreeRTOS_setsockopt( xListeningSocket,
sizeof( xSendTimeOut ) );

/* Bind the socket to port 0x1234. */
xBindAddress.sin_port = FreeRTOS_htons( 0x1234 );
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );

for( ;; )
* The socket can now send and receive data here.


Creating, configuring and binding a UDP socket

<< Back to the RTOS TCP networking tutorial index

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.