我已经为主机所在的子网上的所有客户端编写了一个应该ping(使用isReachable)的应用程序但是当它在XP机器(SP2)上运行时我得到了奇怪的结果它无法获得所有主机。它似乎与线程相关联,就像我放入一个连接并有效地强制应用程序使用它工作的一个线程。它在Windows 7和ubuntu linux中没有连接的情况下运行良好,所以我有点难过为什么它会在XP上崩溃。它现在由两个类别组成。对于java来说也是相当新的,所以希望它没有什么明显的。提前谢谢。
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;
}
}
答案 0 :(得分:0)
已经炮击ping以解决问题它不仅在XP上无法正常工作,isReachable()在ping发现的几台主机上失败了。不是一个理想的解决方案,因为它现在依赖于平台,但无论如何都会运行与Windows相关的WMI查询。
感谢您的光临。