TCP
Sockets
are:
If the socket is used to implement a
server then
call
FreeRTOS_listen() to place the socket into
the Listening state, and call
FreeRTOS_accept()
to accept incoming connections. Source code examples are provided on this
page.
To change the size of the receive and send buffers used by the TCP socket from
their defaults call FreeRTOS_setsockopt()
using the FREERTOS_SO_RCVBUF and
FREERTOS_SO_SNDBUF parameters respectively. This must be done immediately
after the socket is created, before it is connected.
If ipconfigUSE_TCP_WIN
is set to 1 in FreeRTOSIPConfig.h then the
socket will use a sliding window
to minimise overhead and maximise throughput. The size of the sliding
window can be changed from its default using the FREERTOS_SO_WIN_PROPERTIES
parameter to FreeRTOS_setsockopt(). The sliding window size is
specified in units of MSS (so if the MSS is set to 200 bytes then a
sliding window size of 2 is equal to 400 bytes) and must always be
smaller than or equal to the size of the internal buffers in both directions.
By default a child socket is automatically created to handle any connections accepted
on a listening TCP/IP socket (the default behaviour can be changed using
the FREERTOS_SO_REUSE_LISTEN_SOCKET parameter in a call to FreeRTOS_setsockopt()).
Child sockets inherit the buffer sizes and
sliding window sizes of their parent sockets.
The first example below demonstrates how to create, configure and bind
a client socket. The second example below demonstrates how to create,
configure, and bind a server socket, then how to accept new connections on
the server socket.
void vCreateTCPClientSocket( void )
{
Socket_t xClientSocket;
socklen_t xSize = sizeof( freertos_sockaddr );
static const TickType_t xTimeOut = pdMS_TO_TICKS( 2000 );
xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET4,
SOCK_STREAM,
IPPROTO_TCP );
configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
FreeRTOS_setsockopt( xClientSocket,
0,
FREERTOS_SO_RCVTIMEO,
&xTimeOut,
sizeof( xTimeOut ) );
FreeRTOS_setsockopt( xClientSocket,
0,
FREERTOS_SO_SNDTIMEO,
&xTimeOut,
sizeof( xTimeOut ) );
FreeRTOS_bind( xClientSocket, NULL, xSize );
}
Creating, configuring and binding a TCP client socket
void vCreateTCPServerSocket( void )
{
struct freertos_sockaddr xClient, xBindAddress;
Socket_t xListeningSocket, xConnectedSocket;
socklen_t xSize = sizeof( xClient );
static const TickType_t xReceiveTimeOut = portMAX_DELAY;
const BaseType_t xBacklog = 20;
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET4,
SOCK_STREAM,
IPPROTO_TCP );
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
FreeRTOS_setsockopt( xListeningSocket,
0,
FREERTOS_SO_RCVTIMEO,
&xReceiveTimeOut,
sizeof( xReceiveTimeOut ) );
xBindAddress.sin_port = ( uint16_t ) 10000;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
FreeRTOS_listen( xListeningSocket, xBacklog );
for( ;; )
{
xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET );
xTaskCreate( prvServerConnectionInstance,
"EchoServer",
usUsedStackSize,
( void * ) xConnectedSocket,
tskIDLE_PRIORITY,
NULL );
}
}
Creating, configuring and binding a TCP server socket
<<
Back to the RTOS TCP networking tutorial index
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.