我正在用Java寫一個簡單的網絡生存工具。它基本上只需要ping變量地址範圍並將答案保存在數據庫中。Java:線程不會同時ping通
起初,我用java.net庫,但由於它只是使用echo命令,打印機,路由器和某些服務器在網絡中不會回答。所以我使用了icmp4j庫。
既然花了大約10到15分鐘逐一做,當它來ping的254,一個地址範圍,我決定使用線程掃描時間保持在最低限度。然而問題在於,它一次只能打一個地址。
我從來沒有使用線程工作過,所以這裏是代碼,以防萬一我犯了一個巨大的錯誤:
//the AddressRange class just saves the current address and counts it up by one
AddressRange ar = new AddressRange(tFStart.getText(), tFEnd.getText(), false);
//next() checks, if the last address is reached and returns false, if that is the case
while(ar.next()){
try{
//here, I create and start the threads
new Thread(new Ping(ar.getAddress())).start();
} catch (Exception f) {}
//counts up the address by one
ar.countUp(ar.getAddressBits());
}
這裏是我的平安等級:
public class Ping implements Runnable{
private final String address;
public Ping(String address){
this.address = address;
}
@Override
public void run() {
IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
request.setHost(address);
try{
IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
String formattedResponse = IcmpPingUtil.formatResponse(response);
String output = "Destination: " + address + " \n" + formattedResponse + " \n";
if(formattedResponse.contains("Reply")){
InetAddress addr = InetAddress.getByName(address);
output += "Hostname: " + verifyHostName(addr.getHostName()) + " \n";
System.out.println(output);
saveClient(new PingData(output));
}
} catch (Exception f) {}
}
}
當我用java.net庫替換了icmp4j-,線程同時被處理。我讀過,線程不應該訪問相同的資源,所以我想,這就是發生在這裏的事情。但我還沒有經驗來分析甚至重寫庫。
這裏是沒有icmp4j的使用我的平安等級:
public class Ping implements Runnable{
private final String address;
public Ping(String address){
this.address = address;
}
@Override
public void run() {
try{
InetAddress addr = InetAddress.getByName(address);
if(addr.isReachable()){
String output += "Hostname: " + verifyHostName(addr.getHostName()) + " \n";
System.out.println(output);
saveClient(new PingData(output));
}
} catch (Exception f) {}
}
}
所以我想問,如果我犯了一個錯誤,或者如果有人有與所述庫的經驗,知道如何解決這個問題,或者有一個很好的選擇,它完成了工作。
無關:'StringBuilder'就是這樣一個* core *類,它甚至在'java.lang'包中。編寫自己的類並調用它'StringBuilder'將會讓任何讀取你的代碼的人都感到困惑。 – Andreas
謝謝,我不知道。我會糾正它。 :) – Sebowski
我剛剛寫了程序來ping多線程的IP 140和它對icmp4j庫的工作正常,但正如你所說,Java的網絡庫是給予意想不到的結果。在不到一秒鐘的時間裏,我得到了100次ping結果。你是否收到任何錯誤或異常?通過逐個刪除ping步驟徹底調試它! –