经证明,
FreeRTOS_sendto()
TCP/IP 堆栈 API 函数用于向
UDP 套接字发送数据。
只有在
套接字已被
创建、配置并
选择性绑定到本地
端口号后,才能发送数据。
正如 FreeRTOS_sendto()() API 引用页面所述,FreeRTOS_sendto()
可以与标准调用语义或零拷贝调用语义一起使用。
本页演示的是标准调用语义。
下述源代码展示的是 RTOS 任务,该任务在进入循环之前创建了 UDP 套接字,
该循环每秒(1000 毫秒)向套接字发送一个字符串
(使用标准调用语义)。 如需了解更多信息,请参阅源代码示例中的注释
。
static void vUDPSendUsingStandardInterface( void *pvParameters )
{
Socket_t xSocket;
struct freertos_sockaddr xDestinationAddress;
uint8_t cString[ 50 ];
uint32_t ulCount = 0UL;
const TickType_t x1000ms = 1000UL / portTICK_PERIOD_MS;
xDestinationAddress.sin_addr = FreeRTOS_inet_addr( "192.168.0.50" );
xDestinationAddress.sin_port = FreeRTOS_htons( 10000 );
xSocket = FreeRTOS_socket( FREERTOS_AF_INET4,
FREERTOS_SOCK_DGRAM,
FREERTOS_IPPROTO_UDP );
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
for( ;; )
{
sprintf( cString,
"Standard send message number %lurn",
ulCount );
FreeRTOS_sendto( xSocket,
cString,
strlen( cString ),
0,
&xDestinationAddress,
sizeof( xDestinationAddress ) );
ulCount++;
vTaskDelay( x1000ms );
}
}
Example using FreeRTOS_sendto() with the standard (as opposed to zero copy) calling semantics
<<
返回 RTOS TCP 网络教程索引
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.