IPAddress 类

来源:互联网 发布:软件不支持中文字体 编辑:程序博客网 时间:2024/05/19 18:11

IPAddress 类

.NET Framework 4.5
其他版本

提供网际协议 (IP) 地址。

继承层次结构

System.Object 
  System.Net.IPAddress

命名空间:  System.Net
程序集:  System(在 System.dll 中)

语法

C#
C++
F#
VB
[SerializableAttribute]public class IPAddress

IPAddress 类型公开以下成员。

构造函数

 名称说明公共方法IPAddress(Byte[])用指定为 Byte 数组的地址初始化 IPAddress 类的新实例。公共方法IPAddress(Int64)用指定为 Int64 的地址初始化 IPAddress 类的新实例。公共方法IPAddress(Byte[], Int64)用指定为 Byte 数组的地址和指定的范围标识符初始化 IPAddress 类的一个新实例。页首

属性

 名称说明公共属性Address已过时。网际协议 (IP) 地址。公共属性AddressFamily获取 IP 地址的地址族。公共属性IsIPv4MappedToIPv6了解 IP 地址是否为 IPv4 映射的 IPv6 地址。公共属性IsIPv6LinkLocal获取地址是否为 IPv6 链接本地地址。公共属性IsIPv6Multicast获取地址是否为 IPv6 多路广播全局地址。公共属性IsIPv6SiteLocal获取地址是否为 IPv6 站点本地地址。公共属性IsIPv6Teredo获取地址是否为 IPv6 Teredo 地址。公共属性ScopeId获取或设置 IPv6 地址范围标识符。页首

方法

 名称说明公共方法Equals比较两个 IP 地址。 (重写 Object.Equals(Object)。)受保护的方法Finalize允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)公共方法GetAddressBytes以字节数组形式提供 IPAddress 的副本。公共方法GetHashCode返回 IP 地址的哈希值。 (重写 Object.GetHashCode()。)公共方法GetType获取当前实例的 Type (继承自 Object。)公共方法静态成员HostToNetworkOrder(Int16)将短值由主机字节顺序转换为网络字节顺序。公共方法静态成员HostToNetworkOrder(Int32)将整数值由主机字节顺序转换为网络字节顺序。公共方法静态成员HostToNetworkOrder(Int64)将长值由主机字节顺序转换为网络字节顺序。公共方法静态成员IsLoopback指示指定的 IP 地址是否是环回地址。公共方法MapToIPv4将 IPAddress 对象映射到 IPv4 地址。公共方法MapToIPv6将 IPAddress 对象映射到 IPv6 地址。受保护的方法MemberwiseClone创建当前 Object 的浅表副本。 (继承自 Object。)公共方法静态成员NetworkToHostOrder(Int16)将短值由网络字节顺序转换为主机字节顺序。公共方法静态成员NetworkToHostOrder(Int32)将整数值由网络字节顺序转换为主机字节顺序。公共方法静态成员NetworkToHostOrder(Int64)将长值由网络字节顺序转换为主机字节顺序。公共方法静态成员Parse将 IP 地址字符串转换为 IPAddress 实例。公共方法ToString将 Internet 地址转换为标准表示法。 (重写 Object.ToString()。)公共方法静态成员TryParse确定字符串是否为有效的 IP 地址。页首

字段

 名称说明公共字段静态成员Any提供一个 IP 地址,指示服务器应侦听所有网络接口上的客户端活动。 此字段为只读。公共字段静态成员Broadcast提供 IP 广播地址。 此字段为只读。公共字段静态成员IPv6AnySocket.Bind 方法使用 IPv6Any 字段指示 Socket 必须侦听所有网络接口上的客户端活动。公共字段静态成员IPv6Loopback提供 IP 环回地址。 此属性是只读的。公共字段静态成员IPv6None提供指示不应使用任何网络接口的 IP 地址。 此属性是只读的。公共字段静态成员Loopback提供 IP 环回地址。 此字段为只读。公共字段静态成员None提供指示不应使用任何网络接口的 IP 地址。 此字段为只读。页首

备注

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

示例

下面的代码示例显示如何查询服务器以获得服务器支持的族地址及 IP 地址。

C#
C++
VB
// 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: InternNetworkV6. 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 additonal address information.      IPAddressAdditionalInfo();    }  }}

版本信息

.NET Framework

受以下版本支持:4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

平台

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

并不是.NET Framework 对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求.

线程安全

此类型的任何公共static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

请参阅

参考

System.Net 命名空间

社区附加资源


0 0
原创粉丝点击