会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Hello Caviare!
博客园
首页
新随笔
新文章
联系
订阅
管理
判断大陆IPAddress
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.IO;
namespace
IPInfoService
{
public
class
AddressService
{
public
List
<
IPArea
>
listIP
=
new
List
<
IPArea
>
();
/**/
///
<summary>
///
填充大陆IP代码段列表
///
</summary>
public
AddressService()
{
string
iPDBPath
=
@"
D:\ApexLog\eTrade\Clicks\ipDB\country-ipv4.lst
"
;
//
Down loaded from "
http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst
"
StreamReader srIp
=
new
StreamReader(iPDBPath);
while
(
!
srIp.EndOfStream)
{
string
strLine
=
srIp.ReadLine();
int
index
=
strLine.IndexOf(
"
cn
"
);
if
(index
!=
-
1
)
{
string
[] strArr
=
strLine.Split(
'
'
);
listIP.Add(
new
IPArea(strArr[
0
], strArr[
2
]));
}
}
srIp.Close();
}
/**/
///
<summary>
///
该IP是否来自中国大陆
///
</summary>
///
<param name="ipAddress"></param>
///
<returns></returns>
public
bool
ScopeIncludingIpAddress(
string
ipAddress)
{
long
intCurrentIP
=
IpToInt(ipAddress);
for
(
int
i
=
0
; i
<
listIP.Count; i
++
)
{
if
(IpToInt(listIP[i].strIpStart)
<=
intCurrentIP
&&
intCurrentIP
<=
IpToInt(listIP[i].strIpEnd))
{
return
true
;
}
}
return
false
;
}
/**/
///
<summary>
///
IP地址转换成Int数据
///
</summary>
///
<param name="ip"></param>
///
<returns></returns>
private
long
IpToInt(
string
ip)
{
char
[] dot
=
new
char
[]
{
'
.
'
}
;
string
[] ipArr
=
ip.Split(dot);
if
(ipArr.Length
==
3
)
ip
=
ip
+
"
.0
"
;
ipArr
=
ip.Split(dot);
long
ip_Int
=
0
;
long
p1
=
long
.Parse(ipArr[
0
])
*
256
*
256
*
256
;
long
p2
=
long
.Parse(ipArr[
1
])
*
256
*
256
;
long
p3
=
long
.Parse(ipArr[
2
])
*
256
;
long
p4
=
long
.Parse(ipArr[
3
]);
ip_Int
=
p1
+
p2
+
p3
+
p4;
return
ip_Int;
}
}
public
class
IPArea
{
public
string
strIpStart;
public
string
strIpEnd;
public
IPArea(
string
strStart,
string
strEnd)
{
this
.strIpEnd
=
strEnd;
this
.strIpStart
=
strStart;
}
}
}
posted on
2007-11-05 14:00
Caviare
阅读(
458
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部