WSPBind

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                       
);
 

Parameters

s
[in] A descriptor identifying an unbound socket.
name
[in] The address to assign to the socket. The SOCKADDR structure is defined as follows:
sockaddr {
    _short    sa_family;
    char       sa_data[14];
    };
 

Except for the sa_family field, 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

namelen
[in] The length of the name.
lpErrno
[out] A pointer to the error code.

Remarks

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 which 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 shall 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 field 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, but 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.

Return Values

If no error occurs, WSPBind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code is available in lpErrno.

Error Codes

WSAENETDOWN The 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 re-use with SO_REUSEADDR (See the SO_REUSEADDR socket option under WSPSetSockOpt.)
WSAEADDRNOTAVAIL The specified address is not a valid address for this machine.
WSAEFAULT The 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 does not match the address family associated with the socket descriptor s.
WSAEINPROGRESS The function is invoked when a callback is in progress.
WSAEINVAL The socket is already bound to an address.
WSAENOBUFS Not enough buffers available, too many connections.
WSAENOTSOCK The descriptor is not a socket.

QuickInfo

  Windows NT: Yes
  Windows: Yes
  Windows CE: Unsupported.
  Header: Declared in ws2spi.h.

See Also

WSPConnect, WSPListen, WSPGetSockName, WSPSetSockOpt, WSPSocket