Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
nikunj shingala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nikunj shingala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic