IPAddress 类
来源:互联网 发布:软件不支持中文字体 编辑:程序博客网 时间:2024/05/19 18:11
IPAddress 类
.NET Framework 4.5
其他版本
提供网际协议 (IP) 地址。
继承层次结构
System.Object
System.Net.IPAddress
System.Net.IPAddress
程序集: System(在 System.dll 中)
语法
C#
C++
F#
VB
[SerializableAttribute]public class IPAddress
IPAddress 类型公开以下成员。
构造函数
属性
方法
字段
备注
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
- IPAddress类
- IPAddress 类
- IPAddress类的使用
- IPADDRESS
- IpAddress
- IPAddress类与Dns类
- json序列化ipaddress类
- 使用IPAddress类与IPEndPoint类
- IPAddress类/Dns类/IPHostEntry类用法简介
- 笔记10:IPAddress 类、IPInterfaceProperties 类、IPGlobalProperties 类
- IPAddress类的使用方法(C#---网络编程)
- IpAddress算法
- IPAddress.c
- Dns & IPAddress
- IPAddress.Any
- C#中IPAddress类/Dns类/IPHostEntry类/IPEndPoint用法简介
- IPAddress.Any 字段
- linux get eth? ipaddress
- ZOJ 2656 (题目忘了)
- Android downloadmanger 2之对于下载完成和点击下载通知的监听
- 带GPG签名的Git tag
- Windows下安装HBase
- HTTPS那些事儿(二)-实例分析
- IPAddress 类
- css中position的属性
- 编译器DIY——词法分析
- erlang妙用try..catch写出优雅的代码
- Eclipse,到了说再见的时候了——Android Studio最全解析
- linux sysstat 安装使用 及参数说明
- Java集合排序
- struts使用时间标签控件
- 条款30:透彻了解inline的里里外外