低级 Socket 编程中,INADDR_ANY 表示 Socket 可以绑定到任何可用IP地址,这是说它可以绑定到本机任何一个可用的IP地址呢还是说它可以同时绑定到本机所有有效的IP地址?

解决方案 »

  1.   

    一台机器的网络服务当然只能为本机器的网络应用程序提供服务,所以是绑定到本机的ip地址,网络服务提供商根据机器的网络接口配置选择合适的ip地址与其绑定!可以参看MSDN的bind函数中关于ADDR_ANY的描述.
      

  2.   

    If an application does not care what local address is assigned, specify the manifest constant value ADDR_ANY for the sa_data member of the name parameter. This allows the underlying service provider to use any appropriate network address, potentially simplifying application programming in the presence of multihomed hosts (that is, hosts that have more than one network interface and address).
      

  3.   

    默认tcp/ip协议的地址,如果有多个网卡,在配置时其中一个会被设置为默认的,INADDR_ANY就选择这个。
      

  4.   

    On most UNIX systems, it is possible for an application using UDP to filter out broadcasts that it would normally receive. This is accomplished by binding to a network interface explicitly instead of specifying INADDR_ANY. The following code segment illustrates this operation: 
       {
          struct sockaddr_in local;      local.port = htons(5001); // select some port
          local.sin_family = AF_INET;
          local.sin_addr.s_addr = INADDR_ANY;      bind(sockUDP, (struct sockaddr *)&local, sizeof(local) );      /* start doing recvfrom()s here */    } 
    If another application were to issue a broadcast datagram on the same network/subnet, port 5001, the above socket would also receive this datagram. On the other hand, a UNIX-based sockets application would avoid receiving broadcasts by using a network interface address instead of INADDR_ANY. For example: 
    local.sin_addr.s_addr = inet_addr("11.1.1.1"); 
    assuming 11.1.1.1 is a local network interface on the same host. These applications, therefore, do not expect to see broadcasts unless they bind to INADDR_ANY. This is not the expected behavior on Windows NT and Windows 95. On these platforms, an application using UDP datagrams for communication will continue to receive broadcasts aimed at its network, whether or not it binds to INADDR_ANY. Note that an application that binds to a specific interface will not receive datagrams, broadcast or otherwise, that are directed to another interface and its corresponding network/subnet. This behavior is by design.