比较ip是否在两个ip范围之间
2017-08-31 23:50
113 查看
package com.example;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws UnknownHostException {
IpCount[] ipCounts= new IpCount[5];
boolean flag=false;
test test = new test();
ipCounts[0]=new IpCount(1,"192.168.0.1","192.168.0.20");
ipCounts[1]=new IpCount(2,"192.168.0.20","192.168.0.30");
ipCounts[2]=new IpCount(3,"192.168.1.0","192.168.10.1");
ipCounts[3]=new IpCount(4,"192.168.1.100","192.168.1.101");
ipCounts[4]=new IpCount(5,"192.168.0.1","192.168.0.101");
Scanner scanner = new Scanner(System.in);
String st = scanner.nextLine();
for (int i = 0; i < ipCounts.length; i++) {
long startIp = ipTolong(InetAddress.getByName(ipCounts[i].getStart()));
long endIp = ipTolong(InetAddress.getByName(ipCounts[i].getEnd()));
long ipcheck = ipTolong(InetAddress.getByName(st));
if (ipcheck >startIp && ipcheck < endIp) {
flag =true;
break;
} else {
flag =false;
}
}
if (flag==true) {
System.out.println(1);
}else {
System.out.println("-1");
}
}
public static long ipTolong(InetAddress ip){
long result = 0;
byte[] ipAdds=ip.getAddress();
for (byte b : ipAdds) {
result <<= 8;
result |= b&0xff;
}
return result;
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws UnknownHostException {
IpCount[] ipCounts= new IpCount[5];
boolean flag=false;
test test = new test();
ipCounts[0]=new IpCount(1,"192.168.0.1","192.168.0.20");
ipCounts[1]=new IpCount(2,"192.168.0.20","192.168.0.30");
ipCounts[2]=new IpCount(3,"192.168.1.0","192.168.10.1");
ipCounts[3]=new IpCount(4,"192.168.1.100","192.168.1.101");
ipCounts[4]=new IpCount(5,"192.168.0.1","192.168.0.101");
Scanner scanner = new Scanner(System.in);
String st = scanner.nextLine();
for (int i = 0; i < ipCounts.length; i++) {
long startIp = ipTolong(InetAddress.getByName(ipCounts[i].getStart()));
long endIp = ipTolong(InetAddress.getByName(ipCounts[i].getEnd()));
long ipcheck = ipTolong(InetAddress.getByName(st));
if (ipcheck >startIp && ipcheck < endIp) {
flag =true;
break;
} else {
flag =false;
}
}
if (flag==true) {
System.out.println(1);
}else {
System.out.println("-1");
}
}
public static long ipTolong(InetAddress ip){
long result = 0;
byte[] ipAdds=ip.getAddress();
for (byte b : ipAdds) {
result <<= 8;
result |= b&0xff;
}
return result;
}
}
相关文章推荐
- flex 比较二维二元数组是否有重复(包括交集) 例如ip范围的例子
- java 比较某个IP是否在某段IP区间之间
- shell比较两个字符串是否相等
- 通过路由器的IP映射来解决,两个不同IP地址的PC机之间的从LAN口到WAN口的单向通讯问题
- Delphi中比较两个对象是否一致及地址是否相同
- 比较两个字符串是否为变位词
- WithinPastYears、WithinPastMonths、WithinPastWeeks、WithinPastDays ... 判断两个时间差是否在一个指定范围内
- android 简单比较 两个图片是否一致
- mysql 比较两个字符串(是否有包含关系)
- 转:shell比较两个字符串是否相等
- 华为oj:判断两个IP是否属于同一个子网
- [编程题]判断两个IP是否属于同一子网
- VC中比较两个文件是否内容相等
- 怎样比较两个集合是否相等
- JAVA基础知识 比较两个List中内容是否相同
- js判断输入的IP是否合法,IP范围是否正确
- Android中比较两个图片是否想等
- JavaSE8基础 ==和equals 比较两个引用类型的变量的地址值是否相同
- PAT程序能力考题——甲级1010(比较任意进制转换的两个数字,是否相等)
- 判断两个IP是否属于同一子网