//最笨的方法:用多个线程ping每一台主机 import java.io.*; import java.util.*; public class MyScanner{
public MyScanner(){
}
public static int count=0;
public static void main(String args[]) throws Exception{ MyScanner myScanner=new MyScanner();
System.out.println("...start to scan...."); ArrayList<Thread> list=new ArrayList<Thread>(); for(int i=0;i<256;i+=5){ Thread t=new Thread(new ScanThread(i)); t.start(); list.add(t); } for(int i=0;i<list.size();i++){ try{ list.get(i).join(); }catch(Exception e){ e.printStackTrace();} } System.out.println("...end to scan...."); System.out.println("count="+count); } } class ScanThread extends Thread{ public int startIp; public ScanThread(int startIp){ this.startIp=startIp; } public void run(){ Runtime runtime=Runtime.getRuntime(); for(int i=startIp;i<startIp+5;i++){ String ipStr="192.168.132."+i; String cmd="ping "+ipStr; try{ Process proc = runtime.exec(cmd); BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String infor =""; while((infor=theReader.readLine())!=null){ if (infor.startsWith("Reply from")){ MyScanner.count++; System.out.println(ipStr+" is alive!"); break; } else if (infor.startsWith("Request timed out")){ //System.out.println(ipStr+" is dead!"); break; } } theReader.close(); proc.destroy(); }catch(Exception e){e.printStackTrace();} } } }
奇怪了,为什么用了楼上的方法,所有的IP都有主机?可实际上,局域网中只有2台主机啊...start to scan.... 192.168.1.0 is alive! 192.168.1.1 is alive! 192.168.1.75 is alive! 192.168.1.50 is alive! 192.168.1.130 is alive! 192.168.1.35 is alive! 192.168.1.20 is alive! 192.168.1.55 is alive! 192.168.1.5 is alive! 192.168.1.45 is alive! 192.168.1.60 is alive! 192.168.1.65 is alive! 192.168.1.25 is alive! 192.168.1.15 is alive! 192.168.1.40 is alive! 192.168.1.106 is alive! 192.168.1.30 is alive! 192.168.1.115 is alive! 192.168.1.185 is alive! 192.168.1.80 is alive! 192.168.1.120 is alive! 192.168.1.110 is alive! 192.168.1.170 is alive! 192.168.1.145 is alive! 192.168.1.245 is alive! 192.168.1.155 is alive! 192.168.1.90 is alive! . . . ...end to scan.... count=253
//最笨的方法:用多个线程ping每一台主机
import java.io.*;
import java.util.*;
public class MyScanner{
public MyScanner(){
}
public static int count=0;
public static void main(String args[]) throws Exception{ MyScanner myScanner=new MyScanner();
System.out.println("...start to scan....");
ArrayList<Thread> list=new ArrayList<Thread>();
for(int i=0;i<256;i+=5){
Thread t=new Thread(new ScanThread(i));
t.start();
list.add(t);
}
for(int i=0;i<list.size();i++){
try{
list.get(i).join();
}catch(Exception e){ e.printStackTrace();}
}
System.out.println("...end to scan....");
System.out.println("count="+count);
}
}
class ScanThread extends Thread{
public int startIp;
public ScanThread(int startIp){
this.startIp=startIp;
}
public void run(){
Runtime runtime=Runtime.getRuntime();
for(int i=startIp;i<startIp+5;i++){
String ipStr="192.168.132."+i;
String cmd="ping "+ipStr;
try{
Process proc = runtime.exec(cmd);
BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String infor ="";
while((infor=theReader.readLine())!=null){
if (infor.startsWith("Reply from")){
MyScanner.count++;
System.out.println(ipStr+" is alive!");
break;
} else if (infor.startsWith("Request timed out")){
//System.out.println(ipStr+" is dead!");
break;
}
}
theReader.close();
proc.destroy();
}catch(Exception e){e.printStackTrace();}
}
}
}
奇怪了,为什么用了楼上的方法,所有的IP都有主机?可实际上,局域网中只有2台主机啊...start to scan....
192.168.1.0 is alive!
192.168.1.1 is alive!
192.168.1.75 is alive!
192.168.1.50 is alive!
192.168.1.130 is alive!
192.168.1.35 is alive!
192.168.1.20 is alive!
192.168.1.55 is alive!
192.168.1.5 is alive!
192.168.1.45 is alive!
192.168.1.60 is alive!
192.168.1.65 is alive!
192.168.1.25 is alive!
192.168.1.15 is alive!
192.168.1.40 is alive!
192.168.1.106 is alive!
192.168.1.30 is alive!
192.168.1.115 is alive!
192.168.1.185 is alive!
192.168.1.80 is alive!
192.168.1.120 is alive!
192.168.1.110 is alive!
192.168.1.170 is alive!
192.168.1.145 is alive!
192.168.1.245 is alive!
192.168.1.155 is alive!
192.168.1.90 is alive!
.
.
.
...end to scan....
count=253
用循环的又怎么知道ip的起始点和结束点?
记得TCP协议的握手
向局域网广播TCP数据包 看谁抛出异常 抛出异常的都是没开机的 或没有的唉不太懂 看看大牛的