Question about INADDR_ANY

前端 未结 1 1207
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 09:40

The constant INADDR_ANY is the so-called IPv4 wildcard address. The wildcard IP address is useful for applications that bind Internet domain sockets on mu

相关标签:
1条回答
  • 2020-12-18 09:57

    It doesn't get requests for every IP address on the internet(a), it gets requests for every IP address that it services. For example, it may have multiple NICs, each with a separate IP address or it may have a single NIC capable of managing multiple IP addresses (it may even have multiple NICs, each capable of handling multiple IP addresses.

    The key snippet to look at is:

    ... we normally want an application on a multi-homed host to be able to receive datagrams or connection requests that specify any of the host’s IP addresses (my italics).

    In other words, you may have a multi-homed set-up where your machine services 10.0.0.15 and 10.0.0.16. Using INADDR_ANY will allow you to pick up traffic for both those addresses, without picking up requests for 10.0.0.17 which may be the machine on the other end of the bench (or other side of the planet).

    The following table, with the top row being request destinations and the left column being the address you're listening on, shows whether you'll be given a request (Y) or not (N):

    Request to>  10.0.0.15  10.0.0.16  10.0.0.17
    Bind to:    *-------------------------------
    10.0.0.15   |    Y          N          N
    10.0.0.16   |    N          Y          N
    INADDR_ANY  |    Y          Y          N
    

    (a) It doesn't even see the vast majority of requests on the net. The vast majority don't even make it to your nearest router (or probably even your ISP). Even those that do make it to your nearest router, your particular machine might not see if they're destined for another machine on the local segment (promiscuous mode notwithstanding).

    0 讨论(0)
提交回复
热议问题