Developing applications over TCP/IP using Internet sockets

Binding socket names (Internet domain)

A socket is created without a name, but to be used it must be given a name. Until a name is bound to a socket, processes have no way to reference the socket, so no messages may be received on it. Names can be bound explicitly by a process; otherwise, the system assigns a name (this is called an ``implicit bind'') during certain calls, such as connect).

Communicating processes are bound by an association. In the Internet domain, unlike the UNIX domain, an association is composed of local and foreign addresses, and local and foreign ports. In most domains, associations must be unique. In the Internet domain there may never be duplicate <protocol, local address, local port, foreign address, foreign port> tuples. (The term ``tuple'' refers to a joined set, like the term ``pair'' but applying to more than two elements.)

The bind call is used to assign a name to a socket on the local side of a connection:

bind(s, name, namelen);

The argument s is the socket descriptor returned from the socket call. (See ``Creating a socket (Internet domain)''.)

The argument name is a variable length byte string to be interpreted by the supporting protocol or protocols according to the domain type. In the Internet domain, name contains an Internet address and port number, usually formatted in a structure called sockaddr_in. The argument namelen is the length of the name.

You do not have to specify an address unless you want a certain one. To bind an Internet address, the call is:

#include <sys/types.h>
#include <netinet/in.h>
/* ... */
struct sockaddr_in sin;
/* ... */
bind(s, (struct sockaddr *) &sin, sizeof (sin));
To determine what to place in the address sin, see ``Networking library routines used for Internet sockets''. If you set sin to 0, the system binds to the server and returns the address it used.
Next topic: Making a connection (Internet domain)
Previous topic: Socket creation errors

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003