判断一个IP地址是否是本局域网内地址
//
/// <summary>
/// 判断一个IP地址是否是本局域网内地址,是返回true 否则返回false,
/// </summary>
/// <returns></returns>
public static bool IsLocalIP(string strUserIP)
{
bool bIsLocal = false;
if(strUserIP == "127.0.0.1")
{
bIsLocal = true;
}
else
{
string sUserIp = strUserIP;
int nIndexFDot = sUserIp.IndexOf(".",0);
string sFirstSeg = sUserIp.Substring(0,nIndexFDot);
switch(sFirstSeg)
{
case "10":
bIsLocal = true;
break;
case "192":
{
sUserIp = sUserIp.Substring(nIndexFDot+1);
int nIndexSDot = sUserIp.IndexOf(".",0);
string sSecSeg = sUserIp.Substring(0,nIndexSDot);
if(sSecSeg == "168")
{
bIsLocal = true;
}
else
{
bIsLocal = false;
}
break;
}
case "172":
{
sUserIp = sUserIp.Substring(nIndexFDot+1);
int nIndexSDot = sUserIp.IndexOf(".",0);
string sSecSeg = sUserIp.Substring(0,nIndexSDot);
int nSecNum = Convert.ToInt32(sSecSeg);
if((nSecNum >= 16)&&(nSecNum <= 131))
{
bIsLocal = true;
}
else
{
bIsLocal = false;
}
break;
}
default:
bIsLocal = false;
break;
}
}
return bIsLocal;
}
作者:学习靠自己
出处:http://www.cnblogs.com/net064/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
土豪打赏