IPAddress类的使用

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.
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值