DuplicateAddressDetectionState 列舉

定義

指定 IP 位址的目前狀態。

C#
public enum DuplicateAddressDetectionState
繼承
DuplicateAddressDetectionState

欄位

名稱 Description
Deprecated 3

此位址是有效的,但其使用存留期 (Lease Lifetime) 已將要結束,所以不應該由應用程式使用。

Duplicate 2

此位址不是唯一的。 這個位址不應指派給網路介面。

Invalid 0

此位址無效。 無效的位址已過期且不再指派給介面,所以應用程式不應傳送資料封包到該位址。

Preferred 4

此位址有效且其使用不受限制。

Tentative 1

針對位址進行的重複位址偵測程序評估尚未成功完成。 應用程式不應使用該位址,因為它尚未生效且傳送給它的封包會遭捨棄。

範例

下列程式代碼範例會決定單播地址資訊。

C#
public static void DisplayUnicastAddresses()
{
    Console.WriteLine("Unicast Addresses");
    NetworkInterface[] adapters  = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in adapters)
    {
        IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
        UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses;
        if (uniCast.Count >0)
        {
            Console.WriteLine(adapter.Description);
            string lifeTimeFormat = "dddd, MMMM dd, yyyy  hh:mm:ss tt";
            foreach (UnicastIPAddressInformation uni in uniCast)
            {
                DateTime when;

                Console.WriteLine("  Unicast Address ......................... : {0}", uni.Address);
                Console.WriteLine("     Prefix Origin ........................ : {0}", uni.PrefixOrigin);
                Console.WriteLine("     Suffix Origin ........................ : {0}", uni.SuffixOrigin);
                Console.WriteLine("     Duplicate Address Detection .......... : {0}",
                    uni.DuplicateAddressDetectionState);

                // Format the lifetimes as Sunday, February 16, 2003 11:33:44 PM
                // if en-us is the current culture.

                // Calculate the date and time at the end of the lifetimes.
                when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime);
                when = when.ToLocalTime();
                Console.WriteLine("     Valid Life Time ...................... : {0}",
                    when.ToString(lifeTimeFormat,System.Globalization.CultureInfo.CurrentCulture)
                );
                when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressPreferredLifetime);
                when = when.ToLocalTime();
                Console.WriteLine("     Preferred life time .................. : {0}",
                    when.ToString(lifeTimeFormat,System.Globalization.CultureInfo.CurrentCulture)
                );

                when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime);
                when = when.ToLocalTime();
                Console.WriteLine("     DHCP Leased Life Time ................ : {0}",
                    when.ToString(lifeTimeFormat,System.Globalization.CultureInfo.CurrentCulture)
                );
            }
            Console.WriteLine();
        }
    }
}

備註

為了協助確保網路上的所有介面都有唯一的位址,介面主機負責在單播位址上執行「重複位址偵測」演算法。 此演算法的目的是要嘗試防止網路上的非唯一位址。 此程式定義於IETF RFC 1971 中。

MulticastIPAddressInformation 類別會使用此UnicastIPAddressInformation列舉。 當您擷取 物件的單播地址資訊時,會傳回這個類別的 NetworkInterface 實例。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1