Name Binding
The bind call has the following syntax:
err = bind (socket, address, addrlen)
int err, socket, addrlen
struct sockaddr address
The bind call forms an association between
socket that has already
been created, and an address that is specified in
address. The
address is of type sockaddr or one of the subclasses of sockaddr, such
as sockaddr_in (the Internet protocol socket type). This address has
three parts: a family type (AF_INET, AF_UNIX, etc) just as a socket has,
an address part (such as an Internet address) and a port. In the example
program, the family is AF_INET, which tells the operating system the format
of the address and port portions. In this case, the address is zero,
as represented by the INADDR_ANY constant, and the port is 32351, which is
completely arbitrary, but must match the port used by the client.
This is one of the more difficult calls for students to understand since it
doesn't seem to
do anything. However, internally it does quite a bit.
Before the bind call, the socket is like a telephone that hasn't been
assigned a number. You can plug your phone into the wall, but no one will
ever be able to place a call to it. The bind call associates a
port, in effect, giving it a number. The port can be anything from 1024
to 65535 (0-1023 are reserved) and two processes cannot bind the
same port. Note that this does not preclude many processes
connecting
to a port that has been bound by some process.
The port number has no physical significance. It is just the location of
a table entry that describes what to do when something arrives that has the
port as a destination. It allows a machine to connect to lots of different
potential clients, rather than just one at a time. This type of multiplexing,
where a single network connection can logically support many simultaneous
users is vital to making the network useful.
For servers, the address part of the sockaddr structure is often zero,
indicating that any other machine can connect. It is possible to limit
the access to one particular host, but that is seldom done.