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

UDP Sockets are created using the FreeRTOS_socket() API function with the xType (second) parameter set to FREERTOS_SOCK_DGRAM, 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, 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