计算IP地址归属地算法

public class IpTools {

	/**
	 * 判断是校内还是校外IP地址
	 * 
	 * @param sourceIp
	 * @return true 校内 false 校外
	 */
	public boolean ipInOrOut(String sourceIp) {
		long sip = IpStrToLong(sourceIp);
		// 172.0.0.0
		if ((2885681152L < sip) && (sip < 2902458367L)) {
			return true;
		}
		// 10.0.0.0
		if ((167772160L < sip) && (sip < 184549375L)) {
			return true;
		}
		// 172.0.0.0
		if ((3670147072L < sip) && (sip < 3670212607L)) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @param Ip
	 *            源地址
	 * @return IP 的LONG数值
	 */
	public long IpStrToLong(String Ip) {
		String a, b, c, d;
		long es;
		StringTokenizer st = new StringTokenizer(Ip, ".");
		a = (String) st.nextElement();
		b = (String) st.nextElement();
		c = (String) st.nextElement();
		d = (String) st.nextElement();
		es = Long.parseLong(a) * 256 * 256 * 256 + Long.parseLong(b) * 256 * 256 + Long.parseLong(c) * 256
				+ Long.parseLong(d);
		return es;
	}	
}
public class IP_ADDRESS {
 /**
  * @hibernate.id generator-class="assigned"
  */
 private Integer ID;
 /**
  * @hibernate.property 起始IP
  */
 private Float StartIP;
 /**
  * @hibernate.property 终止IP
  */
 private Float EndIP;
 /**
  * @hibernate.property 归属地
  */
 private String Pos;
 /**
  * @hibernate.property 其他(运营商)
  */
 private String Detail;}

 

你可能感兴趣的:(C++,c,Hibernate,算法,C#)