IPAddress 类
- 格式:docx
- 大小:79.04 KB
- 文档页数:9
IPAddress 类.NET Framework (current version)提供网际协议(IP) 地址。
命名空间: 程序集: System(System.dll 中)继承层次结构System.Object.IPAddress语法VB复制<SerializableAttribute>PublicClass IPAddress构造函数名称说明IPAddress(Byte())用指定为Byte数组的地址初始化IPAddress 类的新实例。
IPAddress(Byte(), Int64)用指定为Byte数组的地址和指定的范围标识符初始化IPAddress 类的一个新实例。
IPAddress(Int64)用指定为Int64的地址初始化IPAddress 类的新实例。
属性名称说明Address已过时。
网际协议(IP) 地址。
AddressFamily获取IP 地址的地址族。
IsIPv4MappedToIPv6了解IP 地址是否为IPv4 映射的IPv6 地址。
IsIPv6LinkLocal获取地址是否为IPv6 链接本地地址。
IsIPv6Multicast获取地址是否为IPv6 多路广播全局地址。
IsIPv6SiteLocal获取地址是否为IPv6 站点本地地址。
IsIPv6Teredo获取地址是否为IPv6 Teredo 地址。
ScopeId获取或设置IPv6 地址范围标识符。
方法名称说明Equals(Object)比较两个IP 地址。
(替代Object.Equals(Object)。
)Finalize()在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。
(从Object继承。
)GetAddressBytes()以字节数组形式提供IPAddress 的副本。
GetHashCode()返回IP 地址的哈希值。
(替代Object.GetHashCode()。
)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(String)将IP 地址字符串转换为IPAddress 实例。
ToString()将Internet 地址转换为标准表示法。
(替代Object.ToString()。
)TryParse(String, IPAddress)确定字符串是否为有效的IP 地址。
字段名称说明Any提供一个IP 地址,指示服务器应侦听所有网络接口上的客户端活动。
此字段为只读。
Broadcast提供IP 广播地址。
此字段为只读。
IPv6Any Socket.Bind方法使用IPv6Any字段指示Socket必须侦听所有网络接口上的客户端活动。
IPv6Loopback提供IP 环回地址。
此属性是只读的。
IPv6None提供指示不应使用任何网络接口的IP 地址。
此属性是只读的。
Loopback提供IP 环回地址。
此字段为只读。
None提供指示不应使用任何网络接口的IP 地址。
此字段为只读。
备注IPAddress 类包含计算机在IP 网络上的地址。
示例下面的代码示例显示如何查询服务器以获得服务器支持的族地址及IP 地址。
VB复制' This program shows how to use the IPAddress class to obtain a server ' IP addressess and related information.ImportsSystem Imports Imports .Sockets Imports System.Text.RegularExpressions Imports Microsoft.VisualBasic Namespace Mssc.Services.ConnectionManagementModule M_TestIPAddressClass 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. PrivateSharedSub IPAddresses(ByVal server AsString)TryDim ASCII AsNew System.Text.ASCIIEncoding()' Get server related information.Dim heserver As IPHostEntry = Dns.Resolve(server)' Loop on the AddressListDim curAdd As IPAddressForEach 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() ThenConsole.WriteLine(("Scope Id: " +curAdd.ScopeId.ToString()))EndIf' 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 AsIntegerFor i = 0 To bytes.Length - 1Console.Write(bytes(i))Next iConsole.WriteLine(ControlChars.Cr + ControlChars.Lf)Next curAddCatch e As ExceptionConsole.WriteLine(("[DoResolve] Exception: " +e.ToString()))EndTryEndSub'IPAddresses' This IPAddressAdditionalInfo displays additional server address information.PrivateSharedSub 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())) EndIfConsole.WriteLine(("IsLoopback(Loopback): " + IPAddress.IsLoopback(IPAddress.Loopback).ToString()))Catch e As ExceptionConsole.WriteLine(("[IPAddresses] Exception: " + e.ToString()))EndTryEndSub'IPAddressAdditionalInfoPublicSharedSub Main(ByVal args() AsString)Dim server AsString = 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.'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 AsNew 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)) Elseserver = args(0)IfNot rex.Match(server).Success ThenConsole.WriteLine("Input string format not allowed.")ReturnEndIfEndIf' Get the list of the addresses associated with the requested server.IPAddresses(server)' Get additonal address information.IPAddressAdditionalInfo()EndSub'MainEndClass'TestIPAddressEndModuleEndNamespace版本信息Universal Windows Platform10 后可用.NET Framework1.1 后可用Silverlight2.0 后可用Windows Phone Silverlight7.1 后可用线程安全此类型的任何公共静态(Visual Basic 中为Shared)成员都是线程安全的。