NetSock_Bind
Assign network addresses to sockets. Typically, server sockets bind to addresses but client sockets do not. Servers may bind to one of the local host’s addresses but usually bind to the wildcard address (e.g. NET_SOCK_ADDR_IP_V4_WILDCARD/INADDR_ANY) on a specific, well-known port number. Whereas client sockets usually bind to one of the local host’s addresses but with a random port number (by configuring the socket address structure’s port number field with a value of 0).
Files
net_sock.h/net_sock.cnet_bsd.h/net_bsd.c
Prototypes
NET_SOCK_RTN_CODE NetSock_Bind(NET_SOCK_ID sock_id,
NET_SOCK_ADDR *p_addr_local,
NET_SOCK_ADDR_LEN addr_len,
NET_ERR *p_err);
int bind ( int sock_id,
struct sockaddr *p_addr_local,
socklen_t addr_len);Arguments
sock_id
This is the socket ID returned by NetSock_Open()/socket() when the socket was created.
p_addr_local
Pointer to a socket address structure (see section Network Sockets Concepts) which contains the local host address to bind the socket to.
addr_len
Size of the socket address structure which must be passed the size of the socket address structure [for example, sizeof(NET_SOCK_ADDR_IPv4)].
p_err
Pointer to variable that will receive the return error code from this function:
NET_SOCK_ERR_NONENET_SOCK_ERR_NOT_USEDNET_SOCK_ERR_CLOSEDNET_SOCK_ERR_INVALID_SOCKNET_SOCK_ERR_INVALID_FAMILYNET_SOCK_ERR_INVALID_PROTOCOLNET_SOCK_ERR_INVALID_TYPENET_SOCK_ERR_INVALID_STATENET_SOCK_ERR_INVALID_OPNET_SOCK_ERR_INVALID_ADDRNET_SOCK_ERR_ADDR_IN_USENET_SOCK_ERR_PORT_NBR_NONE_AVAILNET_SOCK_ERR_CONN_FAILNET_IF_ERR_INVALID_IFNET_IPv4_ERR_ADDR_NONE_AVAILNET_IPv4_ERR_ADDR_CFG_IN_PROGRESSNET_IPv6_ERR_ADDR_NONE_AVAILNET_IPv6_ERR_ADDR_TBL_SIZENET_CONN_ERR_NOT_USEDNET_CONN_ERR_NONE_AVAILNET_CONN_ERR_INVALID_CONNNET_CONN_ERR_INVALID_FAMILYNET_CONN_ERR_INVALID_TYPENET_CONN_ERR_INVALID_PROTOCOL_IXNET_CONN_ERR_INVALID_ADDR_LENNET_CONN_ERR_ADDR_NOT_USEDNET_CONN_ERR_ADDR_IN_USENET_INIT_ERR_NOT_COMPLETEDNET_ERR_FAULT_NULL_FNCTNET_ERR_FAULT_LOCK_ACQUIRE
Returned Value
NET_SOCK_BSD_ERR_NONE/0
if successful;
NET_SOCK_BSD_ERR_BIND/-1
otherwise.
Required Configuration
None.
Notes / Warnings
See section Network Sockets Concepts for socket address structure formats.
Sockets may bind to any of the host’s configured addresses, any localhost address (loopback), any link-local address, as well as the wildcard address.
Sockets may bind to specific port numbers or request a random, ephemeral port number by configuring the socket address structure’s port number field with a value of 0. Sockets may not bind to a port number that is within the configured range of random port numbers. By default the base number for random port is 65000:
NET_SOCK_CFG_PORT_NBR_RANDOM_BASE <= RandomPortNbrs <=(NET_SOCK_CFG_PORT_NBR_RANDOM_BASE + NET_SOCK_CFG_NBR_SOCK + 10)