Binding

Each socket needs a unique address. As already stated, an address is the combination of an IP address and a port number.

When a socket is created it assumes the IP address of the network node that created it. If a socket has an IP address but not a port number it is said to be ‘unbound’. An unbound socket cannot receive data because it does not have a complete address.

When a socket has both an IP address and a port number it is said to be ‘bound to a port’, or ‘bound to an address’. A bound socket can receive data because it has a complete address.

The process of allocating a port number to a socket is called ‘binding’.

The API function FreeRTOS_bind() is used to bind a FreeRTOS+TCP socket to a port number.

If ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is set to 0 in FreeRTOSIPConfig.h then FreeRTOS_bind() must be used to bind a socket to a port number before the socket can be used to either send or receive data. If ipconfigALLOW_SOCKET_SEND_WITHOUT_BIND is set to 1 in FreeRTOSIPConfig.h then an unbound socket will be automatically bound to a port number the first time it attempts to send data (for UDP sockets) or connect (for TCP sockets), but can still only receive data after it has been bound.