> What I need is to pass an IP - say 192.168.10.10 - and a subnet for
> example 255.255.255.0
The later is a subnet mask, not a subnet. The term mask is particular
important for your problem. A mask is there to be applied, so apply it.
An algorithm would look like it follows - I am to lazy now to code all
the details and to check for all boundary cases:
Convert both dd representations (IP address and mask) to 32 bit
integers. Then
start = ip & mask;
end = start | (~mask);
If you want to ignore the network number and broadcast address:
start++;
end--;
Then loop from start to end to get all addresses. Be careful with Java's
32 signed integers - that's why I use "!= end" instead of "<= end":
for(int addr = start; addr != end; addr++) {
}
> - and get a list of IP's . I do know binary a
> bit, yet it looks like he's putting a hex value in the for loop, and
> I'm not sure how to calculate it.
The same as any other value. People often don't believe it, but hex,
decimal, octal, etc are just different *representations*. They anyhow
end up as the same binary bit patterns in the computer memory.
> Should I do:
>
> int subnet = ~( ( 1 << n ) - 1 );
>
> Where n == 255.255.255.0
Why? May I suggest that you spend some time to learn how IP addresses
are build and IP subnets are defined (you might want to include CIDR
notation, too). And that you also have a look at binary operations?
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Chris Smith - 24 May 2006 17:14 GMT
> for(int addr = start; addr != end; addr++) {
>
> }
This has a fencepost error. It won't process the last address in the
range. You'd either need to NOT decrement end, or else write a more
complex loop:
boolean done = false;
int addr = start;
while (!done)
{
process(addr);
if (addr == end) done = true;
addr++;
}

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
robert - 24 May 2006 19:07 GMT
> > for(int addr = start; addr != end; addr++) {
> >
[quoted text clipped - 13 lines]
> addr++;
> }
Thanks guys - well except for Thomas's typical abrasive remarks, this
time because I ommitted the word 'mask', and the diatribe about that
hex, octal and binary being the same...
Anyways, this is how the rough draft turned out, for those who google
on it - start++; and end--; not working but that should be easy to
fix:
package cafe;
import static java.lang.System.out;
import java.util.StringTokenizer;
public class GetIPs {
/** Run the client.
* @param args for main() */
public static void main(String[] args) {
try {
doIPs();
out.println("doIPs completed!!!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** Executa a chamada. */
private static void doIPs() {
try {
Integer ip = getIPAsInteger("192.168.200.200");
Integer mask = getIPAsInteger("255.255.0.0");
int start = ip & mask;
int end = start | (~mask);
// Ignore the network number and broadcast address:
start++;
end--;
boolean done = false;
int addr = start;
while (!done) {
getQuadString(addr);
if (addr == end) done = true;
addr++;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Integer getIPAsInteger(String ip) throws Exception {
if (ip == null) {
return null;
}
int result= 0;
StringTokenizer st= new StringTokenizer(ip, ".");
for (int i= 3; i >= 0; i--) {
result |= Integer.parseInt(st.nextToken()) << (i * 8);
}
return new Integer(result);
}
private static String getQuadString(long address) {
StringBuffer sb = new StringBuffer();
for (int i = 0, shift = 24; i < 4; i++, shift -= 8) {
long value = (address >> shift) & 0xff;
sb.append(value);
if (i != 3)
{
sb.append('.');
}
}
out.println(sb.toString());
return sb.toString();
}
}
Robert
Chris Smith - 24 May 2006 19:58 GMT
> Thanks guys - well except for Thomas's typical abrasive remarks, this
> time because I ommitted the word 'mask', and the diatribe about that
> hex, octal and binary being the same...
Nothing in Thomas's post seemed abrasive to me. I regret helping you
now, though. I suspect Thomas does, too.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
robert - 24 May 2006 21:04 GMT
> > Thanks guys - well except for Thomas's typical abrasive remarks, this
> > time because I ommitted the word 'mask', and the diatribe about that
> > hex, octal and binary being the same...
>
> Nothing in Thomas's post seemed abrasive to me. I regret helping you
> now, though. I suspect Thomas does, too.
Reading the post again, I'm in the wrong here. Thanks for pointing that
out, FWIW.
Robert
Chris Uppal - 24 May 2006 21:10 GMT
> Nothing in Thomas's post seemed abrasive to me.
I'm unsure whether it's wise for me to enter this -- is it actually going to
benefit anyone ?
But, FWIW, Thomas's style frequently seems abrasive to me (including the case
in point). Quite often I would call it hostile.
Thomas, I can't offer immediate examples, and anyway I'm not trying to join
any sort of crusade against you, but -- since the question has arisen -- I may
as well say that that's how your responses often strike me. Maybe it's a
language thing....
-- chris