IPAddress 类

定义

提供 Internet 协议 (IP) 地址。

public ref class IPAddress
public class IPAddress
[System.Serializable]
public class IPAddress
type IPAddress = class
[<System.Serializable>]
type IPAddress = class
Public Class IPAddress
继承
IPAddress
属性

示例

下面的代码示例演示如何查询服务器以获取家庭地址及其支持的 IP 地址。


// This program shows how to use the IPAddress class to obtain a server
// IP addressess and related information.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;

namespace Mssc.Services.ConnectionManagement
{

  class TestIPAddress
  {

    /**
      * The IPAddresses method obtains the selected server IP address information.
      * It then displays the type of address family supported by the server and its
      * IP address in standard and byte format.
      **/
    private static void IPAddresses(string server)
    {
      try
      {
        System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();

        // Get server related information.
        IPHostEntry heserver = Dns.GetHostEntry(server);

        // Loop on the AddressList
        foreach (IPAddress curAdd in heserver.AddressList)
        {


          // Display the type of address family supported by the server. If the
          // server is IPv6-enabled this value is: InterNetworkV6. If the server
          // is also IPv4-enabled there will be an additional value of InterNetwork.
          Console.WriteLine("AddressFamily: " + curAdd.AddressFamily.ToString());

          // Display the ScopeId property in case of IPV6 addresses.
          if(curAdd.AddressFamily.ToString() == ProtocolFamily.InterNetworkV6.ToString())
            Console.WriteLine("Scope Id: " + curAdd.ScopeId.ToString());


          // Display the server IP address in the standard format. In
          // IPv4 the format will be dotted-quad notation, in IPv6 it will be
          // in in colon-hexadecimal notation.
          Console.WriteLine("Address: " + curAdd.ToString());

          // Display the server IP address in byte format.
          Console.Write("AddressBytes: ");

          Byte[] bytes = curAdd.GetAddressBytes();
          for (int i = 0; i < bytes.Length; i++)
          {
            Console.Write(bytes[i]);
          }

          Console.WriteLine("\r\n");
        }
      }
      catch (Exception e)
      {
        Console.WriteLine("[DoResolve] Exception: " + e.ToString());
      }
    }

    // This IPAddressAdditionalInfo displays additional server address information.
    private static void IPAddressAdditionalInfo()
    {
      try
      {
        // Display the flags that show if the server supports IPv4 or IPv6
        // address schemas.
        Console.WriteLine("\r\nSupportsIPv4: " + Socket.SupportsIPv4);
        Console.WriteLine("SupportsIPv6: " + Socket.SupportsIPv6);

        if (Socket.SupportsIPv6)
        {
          // Display the server Any address. This IP address indicates that the server
          // should listen for client activity on all network interfaces.
          Console.WriteLine("\r\nIPv6Any: " + IPAddress.IPv6Any.ToString());

          // Display the server loopback address.
          Console.WriteLine("IPv6Loopback: " + IPAddress.IPv6Loopback.ToString());

          // Used during autoconfiguration first phase.
          Console.WriteLine("IPv6None: " + IPAddress.IPv6None.ToString());

          Console.WriteLine("IsLoopback(IPv6Loopback): " + IPAddress.IsLoopback(IPAddress.IPv6Loopback));
        }
        Console.WriteLine("IsLoopback(Loopback): " + IPAddress.IsLoopback(IPAddress.Loopback));
      }
      catch (Exception e)
      {
        Console.WriteLine("[IPAddresses] Exception: " + e.ToString());
      }
    }

    public static void Main(string[] args)
    {
      string server = null;

      // Define a regular expression to parse user's input.
      // This is a security check. It allows only
      // alphanumeric input string between 2 to 40 character long.
      Regex rex = new Regex(@"^[a-zA-Z]\w{1,39}$");

      if (args.Length < 1)
      {
        // If no server name is passed as an argument to this program, use the current
        // server name as default.
        server = Dns.GetHostName();
        Console.WriteLine("Using current host: " + server);
      }
      else
      {
        server = args[0];
        if (!(rex.Match(server)).Success)
        {
          Console.WriteLine("Input string format not allowed.");
          return;
        }
      }

      // Get the list of the addresses associated with the requested server.
      IPAddresses(server);

      // Get additional address information.
      IPAddressAdditionalInfo();
    }
  }
}
' This program shows how to use the IPAddress class to obtain a server 
' IP addressess and related information.
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.RegularExpressions

Namespace Mssc.Services.ConnectionManagement
  Module M_TestIPAddress

    Class TestIPAddress

      'The IPAddresses method obtains the selected server IP address information.
      'It then displays the type of address family supported by the server and 
      'its IP address in standard and byte format.
      Private Shared Sub IPAddresses(ByVal server As String)
        Try
          Dim ASCII As New System.Text.ASCIIEncoding()

          ' Get server related information.
          Dim heserver As IPHostEntry = Dns.Resolve(server)

          ' Loop on the AddressList
          Dim curAdd As IPAddress
          For Each curAdd In heserver.AddressList

            ' Display the type of address family supported by the server. If the
            ' server is IPv6-enabled this value is: InterNetworkV6. If the server
            ' is also IPv4-enabled there will be an additional value of InterNetwork.
            Console.WriteLine(("AddressFamily: " + curAdd.AddressFamily.ToString()))

            ' Display the ScopeId property in case of IPV6 addresses.
            If curAdd.AddressFamily.ToString() = ProtocolFamily.InterNetworkV6.ToString() Then
              Console.WriteLine(("Scope Id: " + curAdd.ScopeId.ToString()))
            End If

            ' Display the server IP address in the standard format. In 
            ' IPv4 the format will be dotted-quad notation, in IPv6 it will be
            ' in in colon-hexadecimal notation.
            Console.WriteLine(("Address: " + curAdd.ToString()))

            ' Display the server IP address in byte format.
            Console.Write("AddressBytes: ")



            Dim bytes As [Byte]() = curAdd.GetAddressBytes()
            Dim i As Integer
            For i = 0 To bytes.Length - 1
              Console.Write(bytes(i))
            Next i
            Console.WriteLine(ControlChars.Cr + ControlChars.Lf)
          Next curAdd 

        Catch e As Exception
          Console.WriteLine(("[DoResolve] Exception: " + e.ToString()))
        End Try
      End Sub


      ' This IPAddressAdditionalInfo displays additional server address information.
      Private Shared Sub IPAddressAdditionalInfo()
        Try
          ' Display the flags that show if the server supports IPv4 or IPv6
          ' address schemas.
          Console.WriteLine((ControlChars.Cr + ControlChars.Lf + "SupportsIPv4: " + Socket.SupportsIPv4.ToString()))
          Console.WriteLine(("SupportsIPv6: " + Socket.SupportsIPv6.ToString()))

          If Socket.SupportsIPv6 Then
            ' Display the server Any address. This IP address indicates that the server 
            ' should listen for client activity on all network interfaces. 
            Console.WriteLine((ControlChars.Cr + ControlChars.Lf + "IPv6Any: " + IPAddress.IPv6Any.ToString()))

            ' Display the server loopback address. 
            Console.WriteLine(("IPv6Loopback: " + IPAddress.IPv6Loopback.ToString()))

            ' Used during autoconfiguration first phase.
            Console.WriteLine(("IPv6None: " + IPAddress.IPv6None.ToString()))

            Console.WriteLine(("IsLoopback(IPv6Loopback): " + IPAddress.IsLoopback(IPAddress.IPv6Loopback).ToString()))
          End If
          Console.WriteLine(("IsLoopback(Loopback): " + IPAddress.IsLoopback(IPAddress.Loopback).ToString()))
        Catch e As Exception
          Console.WriteLine(("[IPAddresses] Exception: " + e.ToString()))
        End Try
      End Sub

      Public Shared Sub Main(ByVal args() As String)
        Dim server As String = Nothing

        ' Define a regular expression to parse user's input.
        ' This is a security check. It allows only
        ' alphanumeric input string between 2 to 40 character long.
        Dim rex As New Regex("^[a-zA-Z]\w{1,39}$")

        If args.Length < 1 Then
          ' If no server name is passed as an argument to this program, use the current 
          ' server name as default.
          server = Dns.GetHostName()
          Console.WriteLine(("Using current host: " + server))
        Else
          server = args(0)
          If Not rex.Match(server).Success Then
            Console.WriteLine("Input string format not allowed.")
            Return
          End If
        End If

        ' Get the list of the addresses associated with the requested server.
        IPAddresses(server)

        ' Get additional address information.
        IPAddressAdditionalInfo()
      End Sub
    End Class
  End Module
End Namespace

注解

IPAddress 类包含 IP 网络上计算机的地址。

构造函数

名称 说明
IPAddress(Byte[], Int64)

使用指定为IPAddress数组的地址和指定的范围标识符初始化类的新实例Byte

IPAddress(Byte[])

使用指定为IPAddress数组的地址初始化类的新实例Byte

IPAddress(Int64)

使用指定为 <a0/a0> 的地址初始化类的新实例

IPAddress(ReadOnlySpan<Byte>, Int64)

使用指定为字节范围和指定范围标识符的地址初始化类的新实例 IPAddress

IPAddress(ReadOnlySpan<Byte>)

使用指定为字节范围的地址初始化类的新实例 IPAddress

字段

名称 说明
Any

提供一个 IP 地址,指示服务器必须侦听所有网络接口上的客户端活动。 此字段是只读的。

Broadcast

提供 IP 广播地址。 此字段是只读的。

IPv6Any

该方法 Bind(EndPoint) 使用 IPv6Any 字段来指示 Socket 必须侦听所有网络接口上的客户端活动。

IPv6Loopback

提供 IP 环回地址。 此属性为只读。

IPv6None

提供一个 IP 地址,指示不应使用网络接口。 此属性为只读。

Loopback

提供 IP 环回地址。 此字段是只读的。

None

提供一个 IP 地址,指示不应使用网络接口。 此字段是只读的。

属性

名称 说明
Address
已过时.
已过时.
已过时.

Internet 协议 (IP) 地址。

AddressFamily

获取 IP 地址的地址系列。

IsIPv4MappedToIPv6

获取 IP 地址是否为 IPv4 映射的 IPv6 地址。

IsIPv6LinkLocal

获取地址是否为 IPv6 链接本地地址。

IsIPv6Multicast

获取地址是否为 IPv6 多播全局地址。

IsIPv6SiteLocal

获取地址是否为 IPv6 站点本地地址。

IsIPv6Teredo

获取地址是否为 IPv6 Teredo 地址。

ScopeId

获取或设置 IPv6 地址范围标识符。

方法

名称 说明
Equals(Object)

比较两个 IP 地址。

GetAddressBytes()

按网络顺序提供作为字节数组的副本 IPAddress

GetHashCode()

返回 IP 地址的哈希值。

GetType()

获取当前实例的 Type

(继承自 Object)
HostToNetworkOrder(Int16)

将短值从主机字节顺序转换为网络字节顺序。

HostToNetworkOrder(Int32)

将整数值从主机字节顺序转换为网络字节顺序。

HostToNetworkOrder(Int64)

将长值从主机字节顺序转换为网络字节顺序。

IsLoopback(IPAddress)

指示指定的 IP 地址是否为环回地址。

MapToIPv4()

IPAddress 对象映射到 IPv4 地址。

MapToIPv6()

IPAddress 对象映射到 IPv6 地址。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
NetworkToHostOrder(Int16)

将短值从网络字节顺序转换为主机字节顺序。

NetworkToHostOrder(Int32)

将整数值从网络字节顺序转换为主机字节顺序。

NetworkToHostOrder(Int64)

将长值从网络字节顺序转换为主机字节顺序。

Parse(ReadOnlySpan<Char>)

将表示为字符范围的 IP 地址转换为 IPAddress 实例。

Parse(String)

将 IP 地址字符串转换为 IPAddress 实例。

ToString()

将 Internet 地址转换为其标准表示法。

TryFormat(Span<Char>, Int32)

尝试将当前 IP 地址的格式设置为所提供的范围。

TryParse(ReadOnlySpan<Char>, IPAddress)

尝试将字符范围分析为值。

TryParse(String, IPAddress)

确定字符串是否为有效的 IP 地址。

TryWriteBytes(Span<Byte>, Int32)

尝试按网络顺序将当前 IP 地址写入字节范围。

适用于