IPAddress,IPEndPoint,IPHostEntry介绍

IPAddress,IPEndPoint,IPHostEntry位于命名空间System.Net下,提供对IP地址的操作

IPAddress是.NET封装的IP地址类

常用方法:

1、IPAddress.Parse()

public static IPAddress Parse (string ipString),这个方法得目的就是将一个ipString转换成为IPAddress类型。

2、IPAddress.Loopback、IPAddress.Broadcast、IPAddress.Any、IPAddress.None都是IPAddress得几个域成员,它们得返回值类型都是IPAddress

class IPAddressSample

{

    public static void Main()

    {

        //创建5个IPAddress对象,并赋值

        IPAddress newaddress1 = IPAddress.Parse("192.168.1.1");//把192.168.1.1转换为IPAddress

        IPAddress newaddress2 = IPAddress.Loopback;//本地环回地址

        IPAddress newaddress3 = IPAddress.Broadcast;//广播地址

        IPAddress newaddress4 = IPAddress.Any;

        IPAddress newaddress5 = IPAddress.None;

 

        /*用System.net.dns类中的GetHostEntry()和GetHostName()方法来建立一个本

        地IP地址,并且建立了一个IPHostEntry对象。用以下两句代码来获取本地IP

        地址*/

 

        IPHostEntry here = Dns.GetHostEntry(Dns.GetHostName());

        IPAddress localaddress = here.AddressList[0];

 

        //判断newaddress2地址是否为环回地址

        if (IPAddress.IsLoopback(newaddress2))

            Console.WriteLine("The Loopback address is: {0}", newaddress2.ToString());

        else

            Console.WriteLine("Error obtaining the loopback address");

 

        //打印本地IP地址

        Console.WriteLine("The Local IP address is: {0}\n", localaddress.ToString());

 

        //判断本地IP地址是否为环回地址

        if (localaddress == newaddress2)

            Console.WriteLine("The loopback address is the same as local address.\n");

        else

            Console.WriteLine("The loopback address is not the local address.\n");

 

        //打印其他IP地址

        Console.WriteLine("The test address is: {0}", newaddress1.ToString());

        Console.WriteLine("Broadcast address: {0}", newaddress3.ToString());

        Console.WriteLine("The ANY address is: {0}", newaddress4.ToString());

        Console.WriteLine("The NONE address is: {0}", newaddress5.ToString());

 

        //用console.readling()使程序在执行完上述代码后不立即退出,在用户输入回车键之后退出程序

 

        Console.ReadLine();

    }


 

IPEndPoint代表网络端点的IP地址和端口号

实例化IPEndPoint

IPAddress newaddress = IPAddress.Parse("192.168.1.1");

 

IPEndPoint ex = new IPEndPoint(newaddress,8000); //创建IPEndPoint实例


常用属性和方法

ex.Address返回IpEndPoint实例的IP地址

ex.Port返回IpEndPoint实例的端口

class IPEndPointSample

{

    public static void Main()

    {

        IPAddress newaddress = IPAddress.Parse("192.168.1.1");

        //创建IPEndPoint实例

        IPEndPoint ex = new IPEndPoint(newaddress,8000);

        Console.WriteLine("The IPEndPoint is:{0}", ex.ToString());

        Console.WriteLine("The AddressFamily is:{0}", ex.AddressFamily);

        Console.WriteLine("The Address is:{0},and the port is:{1}", ex.Address, ex.Port);

        Console.WriteLine("The Min Port Number is:{0}", IPEndPoint.MinPort);

        Console.WriteLine("The Max Port Number is:{0}", IPEndPoint.MaxPort);

        //用port属性单独改变IPEndPoint对象的端口值

        ex.Port = 80;

        Console.WriteLine("The changed IPEndPoint vaule is:{0}", ex.ToString());

        //存储IPEdnPoint实例

        SocketAddress sa = ex.Serialize();

        Console.WriteLine("The Socketaddress is:{0}", sa.ToString());

        Console.ReadLine();

    }

}


IPHostEntry代表某一IP的实体

class IPHostEntryClassSample

{

    public static void Main(string[] argv)

    {

        IPHostEntry results = Dns.GetHostEntry(IPAddress.Parse("127.0.0.1"));

        Console.WriteLine("Host name: {0}", results.HostName);

        foreach (string alias in results.Aliases)

        {

            Console.WriteLine("Alias: {0}", alias);

        }

        foreach (IPAddress address in results.AddressList)

        {

            Console.WriteLine("Address: {0}", address.ToString());

        }

    }

}


 

猜你喜欢

转载自blog.csdn.net/gtosky4u/article/details/49151609