bind
bind associates a local endpoint address with a socket. The endpoint 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 or INADDR6_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( SOCKET s, const struct sockeaddr *name, int namelen );
Parameters
s
Socket descriptor for a socket created by socket.
name
A pointer to an endpoint address, cast to (struct sockaddr *) type to assign to the socket.
namelen
Length of the network address stored in the name parameter, in bytes.
Return Value
0 (zero) if the function succeeds, SOCKET_ERROR if the function fails
Use WSAGetLastError to retrieve a specific error code.
Remarks
If the address is not INADDR_ANY or IN6ADDR_ANY, then the function will try to bind to the address of one of the interfaces configured. In order to bind to a specific IP address, that address must be assigned to an interface.
If family is AF_INET, name points to struct sockaddr_in. If family is AF_INET6, name points to struct sockaddr_in6.
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.
If bind to a link-local IPv6 address is performed, setting Scope ID is not necessary, The TCP/IP stack will automatically assign the correct Scope ID.
Sockets, bound to an IPv6 address also receive UDP packets or incoming TCP connection requests from IPv4 peers.
Multiple sockets can be bound to different IP addresses and the same port.
Multiple sockets can be bound to the same IP address and the same port if the SO_REUSEADDRESS socket option is used on all of them.
Requirements
Minimum Supported Version | RTX64 2013 |
Header | Winsock2.h |
Library | RtTcpip.lib |