Binding Names to Sockets

This section describes the procedure for binding names to sockets.

The socket subroutine creates a socket without a name. An unnamed socket is one without any association to local or destination addresses. Until a name is bound to a socket, processes have no way to reference it and consequently, no message can be received on it.

Communicating processes are bound by an association. The bind subroutine allows a process to specify half of an association: local address, local port, or local path name. The connect and accept subroutines are used to complete a socket's association. Each domain association can have a different composite of addresses. The domain associations are as follows:
Item Description
Internet domain Produces an association composed of local and foreign addresses and local and foreign ports.
UNIX domain Produces an association composed of local and foreign path names.
NDD domain (Network Device Driver of the operating system) Provides an association composed of local device name (operating system NDD name) and foreign addresses, the form of which depends on the protocol being used.

An application program may not care about the local address it uses and may allow the protocol software to select one. This is not true for server processes. Server processes that operate at a well-known port need to be able to specify that port to the system.

In most domains, associations must be unique. Internet domain associations must never include duplicate protocol, local address, local port, foreign address, or foreign port tuples.

UNIX domain sockets need not always be bound to a name, but when bound can never include duplicate protocol, local path name, or foreign path name tuples. The path names cannot refer to files already on the system.

The bind subroutine accepts the Socket, Name, and NameLength parameters. The Socket parameter is the integer descriptor of the socket to be bound. The Name parameter specifies the local address, and the NameLength parameter indicates the length of address in bytes. The local address is defined by a data structure termed sockaddr (see Socket Address Data Structures).

In the Internet domain, a process does not have to bind an address and port number to a socket, because the connect and send subroutines automatically bind an appropriate address if they are used with an unbound socket.

In the NDD domain, a process must bind a local NDD name to a socket.

The bound name is a variable-length byte string that is interpreted by the supporting protocols. Its interpretation can vary from communication domain to communication domain (this is one of the properties of the domain). In the Internet domain, a name contains an Internet address, a length, and a port number. In the UNIX domain, a name contains a path name, a length, and an address family, which is always AF_UNIX.