Platform SDK: Windows Sockets |
The WSPBind function associates a local address (that is, name) with a socket.
int WSPBind ( SOCKET s, const struct sockaddr FAR *name, int namelen, LPINT lpErrno );
sockaddr { _short sa_family; char sa_data[14]; };
Except for the sa_family member, SOCKADDR contents are expressed in network byte order. 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 compatibility. The actual structure is interpreted differently in the context of different address families. The only requirements are that the first u_short is the address family and the total size of the memory buffer in bytes is namelen.
If no error occurs, WSPBind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code is available in lpErrno.
This routine is used on an unconnected connectionless or connection-oriented socket, before subsequent calls to WSPConnect or WSPListen. When a socket is created with WSPSocket, it exists in a name space (address family), but it has no name or local address assigned. WSPBind establishes the local association of the socket by assigning a local name to an unnamed socket.
As an example, in the Internet address family, a name consists of three parts: the address family, a host address, and a port number that identifies the Windows Sockets SPI client. In Windows Sockets 2, the name parameter is not strictly interpreted as a pointer to a SOCKADDR structure. 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 sa_family in the SOCKADDR declaration) must contain the address family that was used to create the socket. Otherwise, the error WSAEFAULT will be indicated.
If a Windows Sockets 2 SPI client does not care what local address is assigned to it, it will specify the manifest constant value ADDR_ANY for the sa_data member of the name parameter. This instructs the service provider to use any appropriate network address. For TCP/IP, if the port is specified as zero, the service provider will assign a unique port to the Windows Sockets SPI client with a value between 1024 and 5000. The SPI client can use WSPGetSockName after WSPBind to learn the address and the port that has been assigned to it. However, note that if the Internet address is equal to INADDR_ANY, WSPGetSockOpt 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.
Error code | Meaning |
---|---|
WSAENETDOWN | Network subsystem has failed. |
WSAEADDRINUSE | Some process on the machine has already bound to the same fully-qualified address (for example, IP address and port in the af_inet case) and the socket has not been marked to allow address reuse with SO_REUSEADDR. (See the SO_REUSEADDR socket option under WSPSetSockOpt.) |
WSAEADDRNOTAVAIL | Specified address is not a valid address for this machine. |
WSAEFAULT | Name or the namelen argument is not a valid part of the user address space, the namelen argument is too small, the name argument contains incorrect address format for the associated address family, or the first two bytes of the memory block specified by name do not match the address family associated with the socket descriptor s. |
WSAEINPROGRESS | Function is invoked when a callback is in progress. |
WSAEINVAL | Socket is already bound to an address. |
WSAENOBUFS | Not enough buffers available, too many connections. |
WSAENOTSOCK | Descriptor is not a socket. |
Version: Requires Windows Sockets 2.0.
Header: Declared in Ws2spi.h.
WSPConnect, WSPListen, WSPGetSockName, WSPSetSockOpt, WSPSocket