|
bind associates a local address with a socket. The network address includes both an IP address and a protocol port number. Servers typically use bind to specify the well-known port at which they will accept connections. The IP address can be set to INADDR_ANY to allow reception from any interface. Alternately, the address of a particular interface can be specified but the port number can be left as zero to allow the stack to choose a port number. This would normally be the case for a client.
Syntax
int bind( __in SOCKET s, __in const struct sockeaddr *name, __in int namelen );
Parameters
s
Socket descriptor for a socket created by socket.
name
Address to assign to the socket from the sockaddr structure.
namelen
Length of the network address stored in the name parameter, in bytes.
Return Values
INVALID_SOCKET if the function fails
Use WSAGetLastError to retrieve a specific error code.
Remarks
If the address is not INADDR_ANY, then the function will try to bind to the address of one of the interfaces configured. This only works for AF_INET family at present. In order to bind to a specific IP address, that address must be assigned to an interface. The exception here is if the IP address is a multicast address, in which case interface IP addresses are irrelevant.
For IPv6, a bind operation cannot be successful until after the duplicate address detection (DAD) process has been completed for the address that is associated with the socket.
NOTE: The bind function does not clean up correctly if an application is terminated abnormally.