aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Java - Java API to find IP Address Range or any Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Java - Java API to find IP Address Range or any Example" Watch "Java - Java API to find IP Address Range or any Example" New topic
Author

Java - Java API to find IP Address Range or any Example

nikunj shingala
Ranch Hand

Joined: Jul 20, 2009
Posts: 30
Hi,

Is there any Java API/code snippet to find the list of all IP Address in a given range? Say example, if i give startIP = 172.30.12.1 and endIP = 172.30.14.255, the output
should be,
172.30.12.1,172.30.12.2,172.30.12.3..................................172.30.12.255,
172.30.13.1,172.30.13.2,172.30.12.3....................................172.30.13.255

I am looking if i can reuse rather than reinvent the wheel.

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Use a byte[] in combination with InetAddress. You initialize your byte[] with the starting address: All you then need is a nested loop that first increases ip[3], then ip[2], etc.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nikunj shingala
Ranch Hand

Joined: Jul 20, 2009
Posts: 30
Thanks but now my issue is that It is not Static means ip rang is dynamic and it is enter by user and if possible then give me full example..???

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

First parse the start and end IP addresses into byte arrays. InetAddress can help you out with that as well: The getByName method also allows you to enter IP addresses, not just host names. You can use a regex to ensure the user passes an IP address first.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java - Java API to find IP Address Range or any Example