The Windows Sockets bind function associates a local address with a socket.
int bind (
const struct sockaddr FAR* name,
[in] A descriptor identifying an unbound socket.
[in] The address to assign to the socket from the SOCKADDR structure.
[in] The length of the name.
The bind function is used on an unconnected socket before subsequent calls to the connect or listen functions. It is used to bind to either connection-oriented (stream) or connectionless (datagram) sockets. When a socket is created with a call to the socket function, it exists in a name space (address family), but it has no name assigned to it. Use bind to establish the local association of the socket by assigning a local name to an unnamed socket.
A name consists of three parts when using the Internet address family: the address family, a host address, and a port number that identifies the application. In Windows Sockets 2, the name parameter is not strictly interpreted as a pointer to a SOCKADDR structure. It is cast this way for Windows Sockets 1.1 compatibility. Service Providers are free to regard it as a pointer to a block of memory of size namelen. The first two bytes in this block (corresponding to the sa_family member of the SOCKADDR structure) must contain the address family that was used to create the socket. Otherwise, an error WSAEFAULT will occur.
If an application does not care what local address is assigned, specify the manifest constant value ADDR_ANY for the sa_data member of the name parameter. This allows the underlying service provider to use any appropriate network address, potentially simplifying application programming in the presence of multihomed hosts (that is, hosts that have more than one network interface and address).
For TCP/IP, if the port is specified as zero, the service provider will assign a unique port to the application with a value between 1024 and 5000. The application can use getsockname after bind to learn the address and the port that has been assigned to it. If the Internet address is equal to INADDR_ANY, getsockname will not necessarily be able to supply the address until the socket is connected, since several addresses can be valid if the host is multihomed. Binding to a specific port number other than port 0 is discouraged for client applications, since there is a danger of conflicting with another socket already using that port number.
Windows CE: For IrSocket implementation of this function:
The Af_irda.h must be explicitly included.
The SOCKADDR_IRDA_wcesdk_SOCKADDR_IRDA structure is used in the addr parameter.
The WSAENETDOWN error value is not supported.
There is no wildcard address equivalent to INADDR_ANY.
IrSockets clients must call bind before using a connect function. If the service name is of the form "LSAP-SELxxx" where xxx is a decimal integer in the range 0-255, the address indicates a specific LSAP-SEL xxx rather than a service name. LSAP-SELxxx service names will cause no IAS calls. The socket will be bound directly to the LSAP-SEL specified bypassing IAS.
If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.
WSANOTINITIALISED A successful WSAStartup must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAEADDRINUSE A process on the machine is already bound to the same fully-qualified address and the socket has not been marked to allow address re-use with SO_REUSEADDR. For example, IP address and port are bound in the af_inet case) . (See the SO_REUSEADDR socket option under setsockopt.)
WSAEADDRNOTAVAIL The specified address is not a valid address for this machine
WSAEFAULT The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains incorrect address format for the associated address family, or the first two bytes of the memory block specified by name does not match the address family associated with the socket descriptor s.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEINVAL The socket is already bound to an address.
WSAENOBUFS Not enough buffers available, too many connections.
WSAENOTSOCK The descriptor is not a socket.
Windows NT: Yes
Windows CE: Use version 1.0 and later.
Header: Declared in winsock2.h.
Import Library: Link with ws2_32.lib.
connect, getsockname, listen, setsockopt, socket, WSACancelBlockingCall