java-get IP Segment collection based on start and end IP

Source: Internet
Author: User

ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.PrintStream; Public classIptest { Public Static voidMain (string[] args) {/*Long IP = Getip2long ("10.0.35.120");        SYSTEM.OUT.PRINTLN ("IP-to" +IP);        Long ip2 = getip2long2 ("10.0.35.120"); System.out.println ("Ip2--and" +ip2);*/fun1 (); }     Public Static Longgetip2long (String IP) {IP=Ip.trim (); String[] IPs= Ip.split ("\ \.")); LongIp2long = 0L;  for(inti = 0; I < 4; ++i) {Ip2long= Ip2long << 8 |Integer.parseint (Ips[i]); }        returnIp2long; }     Public Static Longgetip2long2 (String IP) {IP=Ip.trim (); String[] IPs= Ip.split ("\ \.")); LongIP1 = Integer.parseint (ips[0]); LongIP2 = Integer.parseint (ips[1]); LongIP3 = Integer.parseint (ips[2]); LongIP4 = Integer.parseint (ips[3]); LongIp2long =1l* IP1 * * * * * * * * * * * * * * * * * * * * * * + * + * 256 +IP4; returnIp2long; }     Public Static voidfun1 () {printstream PS=NULL; Try{PS=NewPrintStream (NewFile ("C:\\ip.txt")); } Catch(FileNotFoundException e) {e.printstacktrace (); }        intIpstart,ipend; //String ipstr= "10.46.40.1--10.46.88.127";String ipstr= "1.1.1.1--1.1.1.1"; String sa[]=ipstr.split ("--"); Ipstart=ip2int (sa[0]); Ipend=ip2int (sa[1]); //System.out.println ("Ipstart--" + Ipstart); //System.out.println (ipend);System.out.println (ipend-ipstart+1); String []result=NewString[ipend-ipstart+1];  for(inti=0;i<result.length;i++) {String IP= Int2ip (ipstart+i); System.out.println ("I--" +IP); String[] Iparr= Ip.split ("\ \.")); if("0". Equals (Iparr[3]) | | "255". Equals (iparr[3])){                Continue;        } ps.println (IP); }    }     Public StaticString Int2ip (intl) {        //System.out.println ("l-->" +l);        intA = l/(256*256*256); intb = (l-a*256*256*256)/(256*256); intc = (l-a*256*256*256-b*256*256)/256; intD = (l-a*256*256*256-b*256*256-c*256); //return l/(256*256*256) + "." +l/(256*256) + "." +l/256+ "." +l%256;        returnA + "." +b+ "." +c+ "." +D; }     Public Static intip2int (String IP) {string[] sa=ip.split ("\ \")); returnInteger.parseint (Sa[0]) * (256*256*256) +integer.parseint (sa[1]) * (256*256) +integer.parseint (sa[2]) *256+ Integer.parseint (sa[3]); }}

Java-Gets the collection of IP segments based on the starting and ending IP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.