昨天因工作需要,想找一个IP地址的匹配算法,baidu的结果好少。没办法只好自己做了。
类中的方法:
1、检测IP地址是否合法IP地址;
2、检测IP地址是否在某个范围内。
代码如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public class NetOption
2![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
private enum NET_IP_ERR
4![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
INVALID_IP = 1,
6
EQUAL_IP = 2,
7![](/Images/OutliningIndicators/InBlock.gif)
8
}
9
public NetOption()
10![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
}
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
13
/// 验证字符串是否是合法IP地址(IPV4,不支持通配符)
14
/// </summary>
15
/// <param name="stringIP"></param>
16
/// <returns></returns>
17
public static bool ValidIP(string stringIP)
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
uint ui = 0;
20![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] arr = stringIP.Split(new char[]
{'.'});
21
for (int i = 0 ; i < arr.Length ; i++)
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
if (!uint.TryParse(arr[i],out ui))
24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
return false;
26
}
27
else
28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
if (uint.Parse(arr[i]) <0 || uint.Parse(arr[i]) > 255)
30![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
return false;
32
}
33
}
34
}
35
return true;
36
}
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
38
/// 比较两个IP地址的大小(IPV4,不支持通配符)
39
/// </summary>
40
/// <param name="strIP1">IP地址串1</param>
41
/// <param name="strIP2">IP地址串2</param>
42
/// <returns>相等返回1,IP1小于IP2返回0,IP1大于IP2返回2,IP1或者IP2不是IP地址返回-1</returns>
43
public static int CompareIP(string strIP1,string strIP2)
44![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
if (!ValidIP(strIP1) || !ValidIP(strIP2))
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
return -1;
48
}
49![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] arr1 = strIP1.Split(new char[]
{ '.' });
50![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] arr2 = strIP2.Split(new char[]
{ '.' });
51
if (uint.Parse(arr1[0]) == uint.Parse(arr2[0]))
52![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
if (uint.Parse(arr1[1]) == uint.Parse(arr2[1]))
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
if (uint.Parse(arr1[2]) == uint.Parse(arr2[2]))
56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
if (uint.Parse(arr1[3]) == uint.Parse(arr2[3]))
58![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
return 1;
60
}
61
else
62![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
if (uint.Parse(arr1[3]) < uint.Parse(arr2[3]))
64![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
65
return 0;
66
}
67
else
68![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
return 2;
70
}
71
}
72
}
73
else
74![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
if (uint.Parse(arr1[2]) < uint.Parse(arr2[2]))
76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
return 0;
78
}
79
else
80![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
return 2;
82
}
83
}
84
}
85
else
86![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
if (uint.Parse(arr1[1]) < uint.Parse(arr2[1]))
88![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
return 0;
90
}
91
else
92![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
return 2;
94
}
95
}
96
}
97
else
98![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
if (uint.Parse(arr1[0]) < uint.Parse((arr2[0])))
100![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
return 0;
102
}
103
else
104![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
return 2;
106
}
107
}
108
}
109![](/Images/OutliningIndicators/InBlock.gif)
110![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
111
/// 验证IP地址是否在某个IP地址范围之间
112
/// </summary>
113
/// <param name="SourceIP">需验证的IP地址</param>
114
/// <param name="StartIP">IP范围的起始IP地址</param>
115
/// <param name="EndIP">IP范围的结束IP地址</param>
116
/// <returns></returns>
117
public static bool ValidIPRange(string SourceIP,string StartIP,string EndIP)
118![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
119
if (NetOption.CompareIP(SourceIP,StartIP) >=1 && NetOption.CompareIP(EndIP,SourceIP) >=1)
120![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
return true;
122
}
123
else
124![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
return false;
126
}
127
}
128
}
129
备注:
1、该方法只适用IPv4格式。
2、比较2个IP地址大小的算法使用的字符串比较,效率比较低,有时间可以改成2进制比较。
3、……
类中的方法:
1、检测IP地址是否合法IP地址;
2、检测IP地址是否在某个范围内。
代码如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
108
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
127
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
128
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
129
![](/Images/OutliningIndicators/None.gif)
备注:
1、该方法只适用IPv4格式。
2、比较2个IP地址大小的算法使用的字符串比较,效率比较低,有时间可以改成2进制比较。
3、……