The
FreeRTOS_sendto()
TCP/IP stack API function is used to send data to a
UDP socket.
Data can only be sent after the
socket has been
created, configured, and
optionally bound to a local
port number.
As detailed on the FreeRTOS_sendto() API reference page, FreeRTOS_sendto()
can be used with standard calling semantics, or zero copy calling semantics.
This page demonstrates the zero copy calling semantics.
The source code below shows a RTOS task that creates a UDP socket before entering
a loop that sends a string to the socket (using the standard calling
semantics) every 1 second (1000ms). The comments in the source code
example provide important information on how to use network buffers when
the zero copy interface is used.
IPv4
static void vUDPSendingUsingZeroCopyInterface( void *pvParameters )
{
Socket_t xSocket;
uint8_t *pucBuffer;
struct freertos_sockaddr xDestinationAddress;
BaseType_t lReturned;
uint32_t ulCount = 0UL;
const uint8_t *pucStringToSend = "Zero copy send message number ";
const TickType_t x1000ms = 1000UL / portTICK_PERIOD_MS;
const size_t xStringLength = strlen( ( char * ) pucStringToSend ) + 15;
xDestinationAddress.sin_addr = FreeRTOS_inet_addr( "192.168.0.50" );
xDestinationAddress.sin_port = FreeRTOS_htons( 10000 );
xSocket = FreeRTOS_socket( FREERTOS_AF_INET,
FREERTOS_SOCK_DGRAM,
FREERTOS_IPPROTO_UDP );
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
for( ;; )
{
pucBuffer = FreeRTOS_GetUDPPayloadBuffer_Multi( xStringLength,
portMAX_DELAY,
ipTYPE_IPv4 );
configASSERT( pucBuffer );
memset( pucBuffer, 0x00, xStringLength );
sprintf( pucBuffer, "%s%lurn", ucStringToSend, ulCount );
lReturned = FreeRTOS_sendto( xSocket,
( void * ) pucBuffer,
strlen( ( const char * ) pucBuffer ) + 1,
FREERTOS_ZERO_COPY,
&xDestinationAddress,
sizeof( xDestinationAddress ) );
if( lReturned == 0 )
{
FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucBuffer );
}
else
{
}
ulCount++;
vTaskDelay( x1000ms );
}
}
IPv4 Example using FreeRTOS_sendto() with the zero copy calling semantics
IPv6
static void vUDPSendingUsingZeroCopyInterface( void *pvParameters )
{
Socket_t xSocket;
uint8_t *pucBuffer;
struct freertos_sockaddr xDestinationAddress;
BaseType_t lReturned;
uint32_t ulCount = 0UL;
const uint8_t *pucStringToSend = "Zero copy send message number ";
const TickType_t x1000ms = 1000UL / portTICK_PERIOD_MS;
const size_t xStringLength = strlen( ( char * ) pucStringToSend ) + 15;
BaseType_t rc = FreeRTOS_inet_pton( FREERTOS_AF_INET6, "2001::1234", ( void * ) xDestinationAddress.sin_address.xIP_IPv6.ucBytes );
xDestinationAddress.sin_port = FreeRTOS_htons( 10000 );
xDestinationAddress.sin_family = FREERTOS_AF_INET6;
if( rc == pdPASS )
{
xSocket = FreeRTOS_socket( FREERTOS_AF_INET6,
FREERTOS_SOCK_DGRAM,/*FREERTOS_SOCK_DGRAM for UDP.*/
FREERTOS_IPPROTO_UDP );
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
for( ;; )
{
pucBuffer = FreeRTOS_GetUDPPayloadBuffer_Multi( xStringLength,
portMAX_DELAY,
ipTYPE_IPv6);
configASSERT( pucBuffer );
memset( pucBuffer, 0x00, xStringLength );
sprintf( pucBuffer, "%s%lurn", ucStringToSend, ulCount );
lReturned = FreeRTOS_sendto( xSocket,
( void * ) pucBuffer,
strlen( ( const char * ) pucBuffer ) + 1,
FREERTOS_ZERO_COPY,
&xDestinationAddress,
sizeof( xDestinationAddress ) );
if( lReturned == 0 )
{
FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucBuffer );
}
else
{
}
ulCount++;
vTaskDelay( x1000ms );
}
}
}
IPv6 Example using FreeRTOS_sendto() with the zero copy calling semantics
<<
Back to the RTOS TCP networking tutorial index
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.