这两天要实现一个功能:根据不同地区的客户端IP来获取相对应的服务器IP,以提高上传和下载文件的速度。
配置文件内容:
<appSettings>
<!-- 配置服务器包含的客户端IP段
格式:
服务器IP(或服务器名)|[起始IP-终止IP;][IP地址段;][准确IP地址;]
例:
10.16.0.1|10.16.0.5;
10.16.0.1|10.16.*.*;10.15.*.*;
10.16.0.1|10.13.0.1-10.13.1.1;10.16.*.*;
-->
<add key="ServerIP1" value="10.16.0.1|11.12.12.1-11.12.13.200;10.16.*.*;">
</add>
<add key="ServerIP2" value="20.22.0.1|20.22.*.*;">
</add>
<add key="ServerName" value="1.1.0.1|1.1.0.5;">
</add>
</appSettings>
功能代码如下:
1
public class ServerIP
2
{
3
public ServerIP()
4
{
5
//
6
// TODO: Add constructor logic here
7
//
8
}
9
10
private string GetIPSegment(string IP,int SegmentPosition)
11
{
12
return IP.Split('.')[SegmentPosition-1];
13
}
14
15
private string[] GetIPAreaList(string ConfigIPString)
16
{
17
return ConfigIPString.Split('|')[1].Split(';');
18
}
19
20
private string GetServerIP(string ConfigIPString)
21
{
22
return ConfigIPString.Split('|')[0];
23
}
24
25
private string ConvertIPString(string IP)
26
{
27
string IPString="";
28
for(int i=1;i<=4;i++)
29
{
30
IPString+=GetRepeatString("0",3-GetIPSegment(IP,i).Length)+GetIPSegment(IP,i)+".";
31
}
32
return IPString.Substring(0,IPString.Length-1);
33
}
34
35
private string GetRepeatString(string StringValue,int RepeatCount)
36
{
37
string RepeatStrng="";
38
for(int i=0;i<RepeatCount;i++)
39
{
40
RepeatStrng+=StringValue;
41
}
42
return RepeatStrng;
43
}
44
45
public string GetLocalServerIP(string LocalHostIP)
46
{
47
string[] IPAreaList;
48
for(int i=0;i<ConfigurationSettings.AppSettings.Count;i++)
49
{
50
IPAreaList=GetIPAreaList(ConfigurationSettings.AppSettings[i]);
51
52
for(int j=0;j<IPAreaList.Length;j++)
53
{
54
if(IPAreaList[j]!=null && IPAreaList[j].Trim().Length>0)
55
{
56
if(IPAreaList[j].IndexOf("-")!=-1)
57
{
58
string StartIP=IPAreaList[j].Split('-')[0];
59
string EndIP=IPAreaList[j].Split('-')[1];
60
61
if(Convert.ToInt64(ConvertIPString(LocalHostIP).Replace(".",""))>=Convert.ToInt64(ConvertIPString(StartIP).Replace(".",""))
62
&& Convert.ToInt64(ConvertIPString(LocalHostIP).Replace(".",""))<=Convert.ToInt64(ConvertIPString(EndIP).Replace(".","")))
63
{
64
return GetServerIP(ConfigurationSettings.AppSettings[i]);
65
}
66
}
67
else
68
{
69
string Pattern=("^"+GetIPSegment(ConvertIPString(IPAreaList[j]),1)+"\\."+
70
GetIPSegment(ConvertIPString(IPAreaList[j]),2)+"\\."+
71
GetIPSegment(ConvertIPString(IPAreaList[j]),3)+"\\."+
72
GetIPSegment(ConvertIPString(IPAreaList[j]),4)+"$").Replace("00*","[0-2][0-9][0-9]");
73
MatchCollection Matches=Regex.Matches(ConvertIPString(LocalHostIP),Pattern);
74
if(Matches.Count>0)
75
{
76
return GetServerIP(ConfigurationSettings.AppSettings[i]);
77
}
78
}
79
}
80
}
81
}
82
return "No Found!";
83
}
84
85
}

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

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85
