编程点滴

FOR WEB WINFORM DEVELOP!!
随笔 - 20, 文章 - 0, 评论 - 179, 阅读 - 82704

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

IPAddress,IPEndPoint,IPHostEntry介绍

Posted on   grayboy  阅读(9924)  评论(0编辑  收藏  举报

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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

1
2
3
IPAddress newaddress = IPAddress.Parse("192.168.1.1");
 
IPEndPoint ex = new IPEndPoint(newaddress,8000); //创建IPEndPoint实例

常用属性和方法

 

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

ex.Port返回IpEndPoint实例的端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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的实体

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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());
        }
    }
}

 

实例下载:

IPAddressSample

IPEndPointSample

IPHostEntrySample

 

 

 

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();
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
    }
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示