[comp.unix.questions] INADDR_ANY

coleman@bldrdoc.gov (Sean Coleman 497-5672 ) (06/30/91)

I have noticed that many server programs use INADDR_ANY to 
as the server address when the server binds. Suppose the server
was running on a machine it an IP address of 132.163.128.29 and the
server program sets its address to this instead of INADDR_ANY, what
will happen?

What will happen if the IP address that the server binds to, is
not the IP address of the host. ( I know that this will never
happen if the programmer has the act together but the answer will
help to understand the whole bind operation). I get the feeling
that the address that a program binds to the address that all packets
must have as the receiver for the program to accept the packets.


Thanks 

Sean Coleman
NIST
.

barmar@think.com (Barry Margolin) (06/30/91)

In article <739@dove.nist.gov> coleman@bldrdoc.gov (Sean Coleman 497-5672 ) writes:
>I have noticed that many server programs use INADDR_ANY to 
>as the server address when the server binds. Suppose the server
>was running on a machine it an IP address of 132.163.128.29 and the
>server program sets its address to this instead of INADDR_ANY, what
>will happen?

This should work.  There are a few reasons for using INADDR_ANY.  One is
that it simplifies the program, as it doesn't have to look up the local
address.

A more important reason is that a host can have more than one address; in
fact, most network hosts have at least two IP addresses: 127.0.0.1 (the
semi-standard "localhost" address) and the addresses of each real network
interface.  If the server only specifies one local address, then it will
only respond to connections sent to that specific address.
-- 
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (07/01/91)

In article <1991Jun30.062127.19532@Think.COM> barmar@think.com writes:
  [ reasons to use INADDR_ANY ]
> A more important reason is that a host can have more than one address; in
> fact, most network hosts have at least two IP addresses: 127.0.0.1 (the
> semi-standard "localhost" address) and the addresses of each real network
> interface.  If the server only specifies one local address, then it will
> only respond to connections sent to that specific address.

It is worth noting that the addresses you get back from getsockname()
and getpeername() may not provide full information about the TCP
connection on some machines. If, for instance, you bind to INADDR_ANY
and someone connects to the machine's IP address, getsockname() gives
you INADDR_ANY (typically 0.0.0.0) on some machines and the actual IP
address on others, even though INADDR_ANY is a useless, arguably
incorrect answer. If someone connects to 127.1, you may get not only
127.1, but INADDR_ANY or even the machine's IP address, even though
127.1 is the only right answer. Even if you bind to 127.1 and accept
from 127.1 you may end up getting the machine's IP address, for both the
server and client sides.

Portability can be painful.

---Dan