Binding Names to Sockets

The socket() call creates a socket without a name. An unnamed socket is one without any association to a local address. Until a name is bound to a socket, no messages can be received on it.

Communicating processes are bound by an association. The bind() call allows a process to specify half of an association: local address and local port (TCP/IP), or local path name (NetBIOS and local IPC). The connect() and accept() calls are used to complete a socket's association.

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.

Wildcard addressing is provided to aid local address binding in the Internet domain. When an address is specified as INADDR_ANY (a constant defined in the <NETINET\IN.H> file), the system interprets the address as any valid address.

Sockets with wildcard local addresses may receive messages directed to the specified port number and sent to any of the possible addresses assigned to a host. If a server process wished to connect only hosts on a given network, it would bind the address of the hosts on the appropriate network.

A local port can be specified or left unspecified (denoted by 0), in which case the system selects an appropriate port number for it.

The bind() call accepts the s, name, and namelen parameters. The s parameter is the integer descriptor of the socket to be bound. The name parameter specifies the local address, and the namelen parameter indicates the length of address in bytes. The local address is defined by a data structure termed sockaddr.

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

The bound name is a variable-length byte string that is interpreted by the supporting protocols. Its interpretation can vary from protocol family to protocol family (this is one of the properties of the protocol family).

An example of an application using the bind() call is:
An Application Using the bind() Call

int rc;
int s;
struct sockaddr_in myname;

/* clear the structure */
memset(&myname, 0, sizeof(myname));
myname.sin_len = sizeof(myname);
myname.sin_family = AF_INET;
myname.sin_addr.s_addr = inet_addr("129.5.24.1"); /* specific interface */
myname.sin_port = htons(1024);
...
rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));

For a server in the internet domain to be able to listen for connections on a stream socket or issue recvfrom() on a datagram socket, the server must first bind the socket to a specific address family, local address, and local port. This example binds myname to socket s. Note that the sockaddr_in structure should be zeroed before calling bind(). For a more detailed description, see bind(). For information on the sockaddr_in structure, see Internet Address Formats.

The unique name myname specifies that the application uses an internet address family (AF_INET) at internet address 129.5.24.1, and is bound to port 1024. The preceding example shows two useful network utility routines.

The next figure shows how the bind() call on the server side uses the network utility routine getservbyname() to find a well-known port number for a specified service from the ETC\SERVICES file (for more information on well-known ports, see Ports). The figure also shows the use of the internet address wildcard value INADDR_ANY. This is the value generally used on a socket bind() call. It binds the socket to all internet addresses available on the local machine, without requiring the program to know the local internet address. (The code fragment in the preceding figure will run successfully only on the machine with internet address 192.5.24.1.) If a host has more than one internet address (that is, if it is multihomed host), messages sent to any of the addresses will be deliverable to a socket bound to INADDR_ANY. Abind ( )CallUsingthegetservbyname ( )Call

int rc;
int s;
struct sockaddr_in myname;
struct servent *sp;
...
sp = getservbyname("login","tcp");  /* get application specific */
/* well-known port          */
...
/* clear the structure */
memset(&myname, 0, sizeof(myname));
myname.sin_len = sizeof(myname);
myname.sin_family = AF_INET;
myname.sin_addr.s_addr = INADDR_ANY;  /* any interface */
myname.sin_port = sp->s_port;
...
rc = bind(s,(struct sockaddr *)&myname,sizeof(myname));

See bind() for more on this call.


[Back: Socket Creation]
[Next: Socket Connections]