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.
例子如下:
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.
- IPAddress类的使用
- VC++ ipaddress控件的使用
- MFC中IPADDRESS控件的使用
- MFC中IPADDRESS控件的使用
- MFC中IPADDRESS控件的使用
- IPAddress类
- IPAddress 类
- 使用IPAddress类与IPEndPoint类
- IPADDRESS
- IpAddress
- IPAddress类的使用方法(C#---网络编程)
- IPAddress类与Dns类
- json序列化ipaddress类
- C++/MFC修行之路(8)MFC中IPADDRESS控件的使用
- 使用IPAddress.TryParse()方法校验IP地址时不得不知的“坑”
- IPEndPoint(IPAddress.Any, 0)的意思
- C# 中IPAddress和IPEndPoint的意思
- IPAddress类/Dns类/IPHostEntry类用法简介
- JAVA注释详解
- 常用计算机英语
- 利用.Net泛型实现Tuple
- 字符串操作函数
- 彻底明白 Java 语言中的IO系统
- IPAddress类的使用
- WinForm 的线程安全访问[转]
- 虚拟机linux NAT方式上网图文详解
- WINFORM最小化事件
- 手工编写测试代码(C#)
- wx网罗系列总声明
- C#操作XML文件简要[转]
- C#窗口传值
- VS2005(c#)项目调试问题解决方案[转]