使用.NET测试IP地址是否在地址范围内

编程入门 行业动态 更新时间:2024-10-28 14:24:35
本文介绍了使用.NET测试IP地址是否在地址范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

.NET是否具有IP地址范围的概念?

Does .NET have the notion of IP Address Ranges?

我需要测试给定的IP地址是否在地址范围内.

I need to test if a given IP address is within a range of addresses.

我可以编写一些API来给我类似

I could write some API that would give me something like

IPRange ipRange = IPRange.Parse("127.0.0.1-127.0.0.15"); ipRange.Contains(someAddress);

但是如果已经内置了类似的功能,我不想重新发明轮子.

but I don't want to reinvent the wheel if there is already similar functionality built in.

推荐答案

否,但这是可以完成的操作(VB,因为代码标签未包含在OP中)

No, but here is how it can be done (VB since code tag not included in OP)

'test values Dim rangeStart As Net.IPAddress = Net.IPAddress.Parse("192.168.133.1") Dim rangeEnd As Net.IPAddress = Net.IPAddress.Parse("192.168.133.254") Dim check As Net.IPAddress = Net.IPAddress.Parse("192.168.133.230") 'get the bytes of the address Dim rbs() As Byte = rangeStart.GetAddressBytes Dim rbe() As Byte = rangeEnd.GetAddressBytes Dim cb() As Byte = check.GetAddressBytes 'reverse them for conversion Array.Reverse(rbs) Array.Reverse(rbe) Array.Reverse(cb) 'convert them Dim rs As UInt32 = BitConverter.ToUInt32(rbs, 0) Dim re As UInt32 = BitConverter.ToUInt32(rbe, 0) Dim chk As UInt32 = BitConverter.ToUInt32(cb, 0) 'check If chk >= rs AndAlso chk <= re Then Debug.WriteLine("In Range") Else Debug.WriteLine("Not In Range") End If

更多推荐

使用.NET测试IP地址是否在地址范围内

本文发布于:2023-11-08 02:48:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地址   范围内   测试   NET   IP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!