INADDR_ANY

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Mon Jul 1 07:30:33 AEST 1991


In article <1991Jun30.062127.19532 at Think.COM> barmar at 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



More information about the Comp.unix.questions mailing list