bind

This function associates a local address with a socket.

int bind(
SOCKET s, 
const struct sockaddr *addr, 
int namelen);

Parameters

  • s
    [in] Descriptor that identifies an unbound socket.
  • addr
    [in] Address to assign to the socket from the SOCKADDR structure.
  • namelen
    [in] Length of the name.

Return Values

Zero indicates that no error occurred. SOCKET_ERROR indicates failure. To get a specific error value, call WSAGetLastError.

Remarks

For Infrared Sockets (IrSock), this function has the following additional requirements and behaviors:

  • The Af_irda.h must be explicitly included.
  • The 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 not call bind.
  • If the service name is of the form "LSAP-SELXXX" where XXX is a decimal integer in the range 1-127, the address indicates a specific LSAP-SEL. LSAP-SELXXX service names will cause no IAS calls. The socket will be bound directly to the specified LSAP-SEL, bypassing IAS. Windows CE reserves LSAP-SEL0 for IAS and LSAP-SEL3 for internal use.

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 the bind function 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.

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 assigns a unique port to the application with a value between 1024 and 5000. The application can use getsockname after calling bind to learn the address and the port that has been assigned to it. If the Internet address is equal to INADDR_ANY, getsockname may not necessarily 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.

Requirements

Runs on Versions Defined in Include Link to
Windows CE OS 1.0 and later Winsock.h    

Note   This API is part of the complete Windows CE OS package as provided by Microsoft. The functionality of a particular platform is determined by the original equipment manufacturer (OEM) and some devices may not support this API.

See Also

connect, listen, getsockname, setsockopt, socket, SOCKADDR, SOCKADDR_IN, SOCKADDR_IRDA

 Last updated on Tuesday, July 13, 2004

© 1992-2000 Microsoft Corporation. All rights reserved.