Hi,
I have written an app that should ping (use isReachable) for all the clients on the subnet the host sits on but I'm getting strange results when it is run on an XP machine (SP2) it fails to get all the hosts. It appears to be linked to threading as if I put in a join and effectively force the app to use one thread it works. It works fine in windows 7 and in ubuntu linux without the join so I'm a bit stumped as to why it falls over on XP. It's made up of two classes ill post them now. Also fairly new to java so hopefully it's nothing obvious. Thanks in advance.
Main.java
package subnetping;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
//Set this to your ip and netmask
Network myNetwork = new Network("192.168.1.33", 24);
Thread t = null;
for(String aHost : myNetwork.getClients()){
t = new Thread(new pinger(aHost));
t.start();
// Following makes it work on XP by forcing wait on thread
// try {
// t.join();
// } catch (InterruptedException ex) {
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
// }
}
}
public static class pinger implements Runnable{
private String host;
private InetAddress address;
public pinger(String host){
this.host = host;
try {
address = InetAddress.getByName(host);
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void run(){
try {
if (address.isReachable(3000)) {
System.out.println(host + " reachable");
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Network.java
package subnetping;
import java.util.ArrayList;
import java.util.List;
public class Network {
private String subnet = "";
private int subnetMask;
private List<String> myClients = new ArrayList<String>();
public Network(String hostIP, int subnetMask){
this.subnetMask = subnetMask;
this.subnet = getSubNet(hostIP, this.subnetMask);
createClients();
}
private String ipToBinary(String ipNumber){
String[] temp;
String binOctet1;
String binOctet2;
String binOctet3;
String binOctet4;
String ipAsBinary;
temp = ipNumber.split("\\.");
binOctet1 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[0])));
binOctet2 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[1])));
binOctet3 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[2])));
binOctet4 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[3])));
ipAsBinary = binOctet1 + binOctet2 + binOctet3 + binOctet4;
return ipAsBinary;
}
private String binaryToIP(String binaryIP){
return longToIP(Long.parseLong(binaryIP, 2));
}
private Long ipToLong (String ipNumber){
String[] temp;
String binOctet1;
String binOctet2;
String binOctet3;
String binOctet4;
Long ipAsLong;
temp = ipNumber.split("\\.");
binOctet1 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[0])));
binOctet2 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[1])));
binOctet3 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[2])));
binOctet4 = padLeftToEight(Integer.toBinaryString(Integer.parseInt(temp[3])));
ipAsLong = Long.parseLong((binOctet1 + binOctet2 + binOctet3 + binOctet4),2);
return ipAsLong;
}
private String longToIP (Long LongIP){
String binOctet1;
String binOctet2;
String binOctet3;
String binOctet4;
int intOctet1;
int intOctet2;
int intOctet3;
int intOctet4;
String fullBin;
String ipAsString;
fullBin = padLeftToThirtyTwo(Long.toBinaryString(LongIP));
binOctet1 = fullBin.substring(0, 8);
binOctet2 = fullBin.substring(8, 16);
binOctet3 = fullBin.substring(16, 24);
binOctet4 = fullBin.substring(24);
intOctet1 = Integer.parseInt(binOctet1, 2);
intOctet2 = Integer.parseInt(binOctet2, 2);
intOctet3 = Integer.parseInt(binOctet3, 2);
intOctet4 = Integer.parseInt(binOctet4, 2);
ipAsString = intOctet1 + "." + intOctet2 + "." + intOctet3 + "." + intOctet4;
return ipAsString;
}
private String padLeftToEight(String octet){
String paddedOctet = octet;
if(octet.length() == 8){
return octet;
}else{
for( int i = 1; i <= (8 - octet.length()); i++){
paddedOctet = "0" + paddedOctet;
}
return paddedOctet;
}
}
private String padLeftToThirtyTwo(String ipNumber){
String paddedIPNumber = ipNumber;
if(ipNumber.length() == 32){
return ipNumber;
}else{
for( int i = 1; i <= (32 - ipNumber.length()); i++){
paddedIPNumber = "0" + paddedIPNumber;
}
return paddedIPNumber;
}
}
private String padRightToThirtyTwo(String ipNumber){
String paddedIPNumber = ipNumber;
if(ipNumber.length() == 32){
return ipNumber;
}else{
for( int i = 1; i <= (32 - ipNumber.length()); i++){
paddedIPNumber = paddedIPNumber + "0";
}
return paddedIPNumber;
}
}
private String getSubNet(String ipNumber, int subnetMask){
for(int i = 0; i < subnetMask; i++){
subnet = subnet + ipToBinary(ipNumber).charAt(i);
}
return binaryToIP(padRightToThirtyTwo(subnet));
}
private void createClients(){
long subnetLong;
long clientRange;
clientRange = ((long) Math.pow(2L, (32L - subnetMask)) - 2);
subnetLong = ipToLong(this.subnet);
for(int i = 1; i <= clientRange; i ++){
myClients.add(longToIP(subnetLong + i));
}
}
public List<String> getClients(){
return myClients;
}
}