IPAddress类的使用

来源:互联网 发布:php就业如何 编辑:程序博客网 时间:2024/05/21 22:37
C# .NET中得IPAddress类
例子如下:
using System;
using System.Net;


class AddressSample
{
    public static void Main()
    {
    IPAddress test1 = IPAddress.Parse("192.168.1.1");//192.168.1.1
    IPAddress test2 = IPAddress.Loopback;//127.0.0.1
    IPAddress test3 = IPAddress.Broadcast;//255.255.255.255
    IPAddress test4 = IPAddress.Any;//0.0.0.0
    IPAddress test5 = IPAddress.None;//255.255.255.255

    IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
    IPAddress myself = ihe.AddressList[0];//202.115.203.199

    if (IPAddress.IsLoopback(test2))
        Console.WriteLine("The Loopback address is: {0}", test2.ToString());
    else
        Console.WriteLine("Error obtaining the loopback address");

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

    if (myself == test2)
        Console.WriteLine("The loopback address is the same as local address./n");
    else
        Console.WriteLine("The loopback address is not the local address./n");

    Console.WriteLine("The test address is: {0}", test1.ToString());
    Console.WriteLine("Broadcast address: {0}", test3.ToString());
    Console.WriteLine("The ANY address is: {0}", test4.ToString());
    Console.WriteLine("The NONE address is: {0}", test5.ToString());
    }
}

好了,现在 来将一下里面用得几个方法:
1、IPAddress.Parse(),方法,在MSDN中这样解释它:
   public static IPAddress Parse (string ipString),这个方法得目的就是将一个ipString转换成为IPAddress类型。
   这个方法很简单就可以理解了!
2、IPAddress.Loopback、IPAddress.Broadcast、IPAddress.Any、IPAddress.None都是IPAddress得几个域成员,它们得返回值类型都是IPAddress。

a、IPAddress.Loopback Provides the IP loopback address.
   This field is read-only.The Loopback field is equivalent to 127.0.0.1 in dotted-quad notation.
  
b、IPAddress.Broadcast Provides the IP broadcast address. This field is read-only.
   The Broadcast field is equivalent to 255.255.255.255 in dotted-quad notation.
c、IPAddress.Any Provides an IP address that indicates that the server must listen for client activity on all network interfaces. This field is    read-only.
   The Socket.Bind method uses the Any field to indicate that a Socket instance must listen for client activity on all network interfaces.
d、IPAddress.None  Provides an IP address that indicates that no network interface should be used. This field is read-only.
   The Socket.Bind method uses the None field to indicate that a Socket must not listen for client activity. The None field is equivalent to      

255.255.255.255 in dotted-quad notation.
 
原创粉丝点击