Create, Configure and Bind a TCP Socket
Part of the FreeRTOS+TCP Networking Tutorial
Created using the FreeRTOS_socket()
API function with the xType (second) parameter set to FREERTOS_SOCK_STREAM,
Configured using the FreeRTOS_setsockopt()
- and bound to a port using the FreeRTOS_bind() function.
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.