[FreeRTOS+TCP API Reference]
BaseType_t FreeRTOS_bind( Socket_t xSocket, struct freertos_sockaddr *pxAddress, socklen_t xAddressLength );Binds a socket to a local port number. Binding a socket associates a socket with a port number on the local IP address, resulting in the socket receiving all the data that is sent to that IP address and port number combination.
The Network Addressing and Binding sections of the Embedded Networking Basics and Glossary page provide an introduction to the topic of socket binding.
Port numbers above 49408 (0xC100) to 65280 (0xff00) are considered private numbers available to the IP stack for dynamic allocation, and should therefore be avoided. Specifying a port number of 0 or passing pxAddress as NULL will result in the socket being bound to a port number from the private range.
For convenience, if ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is set to 1 in FreeRTOSIPConfig.h, then calling FreeRTOS_send() on a socket that has not first been bound to a port number will also result in the socket being bound to a port number from the private range.
FreeRTOS+TCP does not [currently] use all the function parameters. The parameters that are not used are retained in the function’s prototype to ensure consistency with the expected standard Berkeley sockets API, and to ensure compatibility with future versions of FreeRTOS+TCP.
The handle of the socket that is being bound to an address. The socket
must have previously been created by a successful call to
A pointer to a freertos_sockaddr structure that
contains the details of the port number being bound to.
See the provided example.
Not currently used, but should be set to
sizeof( struct freertos_sockaddr ) to ensure future
If the bind was successful then 0 is returned (0 is the standard Berkeley sockets success return value, contrary to the FreeRTOS standard where 0 means fail!).
-FREERTOS_EINVAL is returned if the socket did not get bound, probably because the specified port number was already in use.
-FREERTOS_ECANCELED is returned if the calling RTOS task did not get a response from the IP RTOS task to the bind request.
See the examples on the FreeRTOS+TCP networking tutorial page, and on the FreeRTOS_socket() API documentation page.