[Note: This page does not describe the callback or zero copy interfaces,
which are available for expert users.]
After a TCP socket has been
created, configured, and bound
it can be connected to a remote socket using the
FreeRTOS_connect() API
function, or it can accept
connections from a remote socket. Once
connected, data is sent to the remote socket using the
FreeRTOS_send() API
function.
The source code snippets below show a function that creates a socket, sends data
to the socket, then gracefully shuts down and closes the socket. Both IPv4 and IPv6 use cases
are shown. Note
that this socket is not explicitly bound to a port number - causing it
to be bound automatically inside the FreeRTOS_connect() API function.
IPv4
void vTCPSend( char *pcBufferToTransmit, const size_t xTotalLengthToSend )
{
Socket_t xSocket;
struct freertos_sockaddr xRemoteAddress;
BaseType_t xAlreadyTransmitted = 0, xBytesSent = 0;
TaskHandle_t xRxTask = NULL;
size_t xLenToSend;
xRemoteAddress.sin_port = FreeRTOS_htons( 1500 );
xRemoteAddress.sin_addr = FreeRTOS_inet_addr_quick( 192, 168, 0, 200 );
xSocket = FreeRTOS_socket( FREERTOS_AF_INET,
FREERTOS_SOCK_STREAM,
FREERTOS_IPPROTO_TCP );
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
if( FreeRTOS_connect( xSocket, &xRemoteAddress, sizeof( xRemoteAddress ) ) == 0 )
{
while( xAlreadyTransmitted < xTotalLengthToSend )
{
xLenToSend = xTotalLengthToSend - xAlreadyTransmitted;
xBytesSent = FreeRTOS_send(
xSocket,
&( pcBufferToTransmit[ xAlreadyTransmitted ] ),
xLenToSend,
0 );
if( xBytesSent >= 0 )
{
xAlreadyTransmitted += xBytesSent;
}
else
{
break;
}
}
}
FreeRTOS_shutdown( xSocket, FREERTOS_SHUT_RDWR );
while( FreeRTOS_recv( xSocket, pcBufferToTransmit, xTotalLengthToSend, 0 ) >= 0 )
{
vTaskDelay( pdTICKS_TO_MS( 250 ) );
}
FreeRTOS_closesocket( xSocket );
}
Example using FreeRTOS_send()
IPv6
void vTCPSend( char *pcBufferToTransmit, const size_t xTotalLengthToSend )
{
Socket_t xSocket;
struct freertos_sockaddr xRemoteAddress;
BaseType_t xAlreadyTransmitted = 0, xBytesSent = 0;
TaskHandle_t xRxTask = NULL;
size_t xLenToSend;
xRemoteAddress.sin_port = FreeRTOS_htons( 1500 );
FreeRTOS_inet_pton6( "2001:470:ed44::9c08:38cc:599f:f62a", (void *) xRemoteAddress.sin_address.xIP_IPv6.ucBytes )
xSocket = FreeRTOS_socket( FREERTOS_AF_INET6,
FREERTOS_SOCK_STREAM,
FREERTOS_IPPROTO_TCP );
configASSERT( xSocket != FREERTOS_INVALID_SOCKET );
if( FreeRTOS_connect( xSocket, &xRemoteAddress, sizeof( xRemoteAddress ) ) == 0 )
{
while( xAlreadyTransmitted < xTotalLengthToSend )
{
xLenToSend = xTotalLengthToSend - xAlreadyTransmitted;
xBytesSent = FreeRTOS_send(
xSocket,
&( pcBufferToTransmit[ xAlreadyTransmitted ] ),
xLenToSend,
/* ulFlags. */
0 );
if( xBytesSent >= 0 )
{
xAlreadyTransmitted += xBytesSent;
}
else
{
break;
}
}
}
FreeRTOS_shutdown( xSocket, FREERTOS_SHUT_RDWR );
while( FreeRTOS_recv( xSocket, pcBufferToTransmit, xTotalLengthToSend, 0 ) >= 0 )
{
vTaskDelay( pdTICKS_TO_MS( 250 ) );
}
FreeRTOS_closesocket( xSocket );
}
Example using FreeRTOS_send()
<<
Back to the RTOS TCP networking tutorial index
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.