Download FreeRTOS
 

Quality RTOS & Embedded Software

LIBRARIES
WHAT'S NEW
Simplifying Authenticated Cloud Connectivity for Any Device.
Designing an energy efficient and cloud-connected IoT solution with CoAP.
Introducing FreeRTOS Kernel version 11.0.0:
FreeRTOS Roadmap and Code Contribution process.
OPC-UA over TSN with FreeRTOS.

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,
/* Use FREERTOS_AF_INET6 for IPv6 UDP socket */
FREERTOS_SOCK_DGRAM,
/*FREERTOS_SOCK_DGRAM for UDP.*/
FREERTOS_IPPROTO_UDP );

/* 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,
0,
FREERTOS_SO_SNDTIMEO,
&xSendTimeOut,
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.