Solved

Get all ips with a given range

Posted on 2003-12-11
2
337 Views
Last Modified: 2007-12-19
I am a newbie so please be patient.  I have an IP range in which I would like to find all possible IPs within that range.  Also I'd like to be able to compare ip values in order to see which has a higher number.  I have not been able to find a class that enables me to do such.  Anyone know of anything?
0
Comment
Question by:tsr50
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 9923124
You're welcome to use this to convert between numbers and ip addresses, from which you can construct a range:

import java.net.InetAddress;

public class InetRange {

  public static void main(String[] args) {
    long ip = InetRange.ipToLong(args[0]);
    System.out.println(ip);
    System.out.println(longToIp(ip));
  }


  public static String longToIp(long ipAddress) {
     StringBuffer sb = new StringBuffer(15);
     long octetMask = 0xFF;
     long octet1 = (ipAddress >> 24) & octetMask;
     long octet2 = (ipAddress >> 16) & octetMask;
     long octet3 = (ipAddress >>  8) & octetMask;
     long octet4 = (ipAddress >>  0) & octetMask;
     sb.append(octet1).append('.');
     sb.append(octet2).append('.');
     sb.append(octet3).append('.');
     sb.append(octet4);
     return sb.toString();
  }


  public static long ipToLong(String ipAddress) {
      long result = 0;
      try {
           byte[] bytes = InetAddress.getByName(ipAddress).getAddress();
           long octet1 = bytes[0] & 0xFF;
           octet1 <<= 24;
           long octet2 = bytes[1] & 0xFF;
           octet2 <<= 16;
           long octet3 = bytes[2] & 0xFF;
           octet3 <<= 8;
           long octet4 = bytes[3] & 0xFF;
           result = octet1 | octet2 | octet3 | octet4;
      }
      catch(Exception e) {
           e.printStackTrace();
           return 0;
      }
      return result;
  }




  public static String intToIp(int ipAddress) {
    int octet1 = (ipAddress & 0xFF000000) >> 24;
    int octet2 = (ipAddress & 0xFF0000) >> 16;
    int octet3 = (ipAddress & 0xFF00) >> 8;
    int octet4 = ipAddress & 0xFF;
    return new StringBuffer().append(octet1)
                             .append('.')
                             .append(octet2)
                             .append('.')
                             .append(octet3)
                             .append('.')
                             .append(octet4)
                             .toString();
  }

}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9930242
8-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now