none
为什么interfaceindex得到的全是0 RRS feed

  • 常规讨论

  • 我参考了下面网页上的代码,
    http://msdn.microsoft.com/en-us/library/system.net.sockets.ipv6multicastoption.interfaceindex.aspx
    写了下面的代码, 得到的InterfaceIndex 全是0?
                IPAddress[] addrGrp;
                string strHostname = Dns.GetHostName();
                // Get all IPAdresses by host name.
                addrGrp = Dns.GetHostAddresses(strHostname);
                // Exercise the use of the IPv6MulticastOption.
                Console.WriteLine("Instantiate IPv6MulticastOption(IPAddress) @ " + strHostname + "\n");
               
                foreach (IPAddress addr in addrGrp)
                {
                    // Instantiate IPv6MulticastOption using one of the overloaded constructors.
                    IPv6MulticastOption ipv6MulticastOption = new IPv6MulticastOption(addr);
                    // Store the IPAdress multicast options.
                    IPAddress group = ipv6MulticastOption.Group;
                    long interfaceIndex = ipv6MulticastOption.InterfaceIndex;
                    // Display IPv6MulticastOption properties.
                    Console.WriteLine("\tIPAddress: [" + group + "]");
                    Console.WriteLine("\tInterfaceIndex: [" + interfaceIndex + "]");
                }

    这是怎么回事? InterfaceIndex是不是IPv6MulticastOption构造函数里指定的? 这里没有指定, 所以为0?
    那怎么获取InterfaceIndex? 谢谢!
    2009年9月17日 5:44

全部回复

  • 你好 如你所述interfaceindex是你自己在构造函数中指定的
    你可以测试下面这段代码
    private static void TestIP()
          {
             IPAddress[] addrGrp;
             string strHostname = Dns.GetHostName();
             // Get all IPAdresses by host name.
             addrGrp = Dns.GetHostAddresses(strHostname);
             // Exercise the use of the IPv6MulticastOption.
             Console.WriteLine("Instantiate IPv6MulticastOption(IPAddress) @ " + strHostname + "\n");
             int i = 0;
             foreach (IPAddress addr in addrGrp)
             {
                // Instantiate IPv6MulticastOption using one of the overloaded constructors.
                IPv6MulticastOption ipv6MulticastOption = new IPv6MulticastOption(addr,i);
                // Store the IPAdress multicast options.
                IPAddress group = ipv6MulticastOption.Group;
                long interfaceIndex = ipv6MulticastOption.InterfaceIndex;
                // Display IPv6MulticastOption properties.
                Console.WriteLine("\tIPAddress: [" + group + "]");
                Console.WriteLine("\tInterfaceIndex: [" + interfaceIndex + "]");
                i++;
             }
          }

    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond
    2009年9月17日 6:45
    版主
  • 多谢! 我不明白那这个InterfaceIndex有什么意义? 我在网页
    http://technet.microsoft.com/en-us/library/bb726952.aspx
    上看到如下文字:

    For link-local addresses, the zone ID is typically the interface index of the interface either assigned the address or to be used as the sending interface for a link-local destination. The interface index is an integer starting at 1 that is assigned to IPv6 interfaces, which include a loopback and one or multiple tunnel or LAN interfaces. You can view the list of interface indexes from the display of the netsh interface ipv6 show interface command.


    其中的interface index和ipv6MulticastOption.InterfaceIndex 不是一个东西? 因为用netsh interface ipv6 show interface 命令得到的Interface Index和代码里面的显然不一样. 感觉代码里得到的(如你代码中的变量 i ) 只是addrGrp数组的索引. 不知理解是否正确?
    2009年9月17日 7:56