使用java线程扫描局域网ip简单方案

from: http://wooden-baby.iteye.com/blog/351912
使用java线程扫描局域网ip简单方案

java 快速扫描局域网 ip 之二级嵌套类

方案一 :

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.ArrayList;



public class LanIP {

       public ArrayList<String> getLanIPArrayList() {

              ArrayList<String> arrayIP = null;

              try {

                     InitSystem initSystem = null;

                     initSystem = new InitSystem();

                     Thread thread = new Thread(initSystem);

                     thread.start();

                     thread.join();

                     arrayIP = initSystem.getArrayIPUsed();

              } catch (UnknownHostException e) {

                     e.printStackTrace();

              } catch (InterruptedException e) {

                     e.printStackTrace();

              }

              return arrayIP;

       }



       private class InitSystem implements Runnable {

              private int firstIP = 2;// 查询的 IP 地址的最后一位起始点

              private int lastIP = 255;// 查询的 IP 地址的最后一位结束点

              private volatile ArrayList<Thread> arrayThread;// 子线程段

              private final int MAXTHREADNUM = 30; // 最大同时进行的子线程数量

              private int threadNumNow;// 当前正在进行的子线程数量

              private volatile ArrayList<String> arrayIP;// 局域网查询所有可能的 IP 地址的结果集

              private volatile ArrayList<String> arrayIPUsed;// 局域网查询已经使用的 IP 地址的结果集



              private InitSystem(String ip) {

                     arrayIP = new ArrayList<String>();

                     arrayIPUsed = new ArrayList<String>();

                     arrayThread = new ArrayList<Thread>();

                     setIPAddressList(ip);

              }



              private InitSystem() throws UnknownHostException {

                     this(InetAddress.getLocalHost().getHostAddress());

              }



              private synchronized ArrayList<String> getArrayIPUsed() {

                     try {

                            while (arrayIP.size() > 0) {

                                   Thread.sleep(300);

                            }

                     } catch (InterruptedException e) {

                            e.printStackTrace();

                     }

                     return arrayIPUsed;

              }



              private void setIPAddressList(String ip) {

                     // 根据这个 ip 地址查询它所在的局域网的所有可能 IP 地址的集合

                     int lastPointIndex = ip.lastIndexOf('.');

                     String stringIPHead = ip.substring(0, ++lastPointIndex);

                     String stringIP = null;

                     for (int i = firstIP; i <= lastIP; i++) {

                            stringIP = stringIPHead + i;

                            arrayIP.add(stringIP);

                     }

              }



              public void run() {

                     synchronized (this) {

                            try {

                                   while (arrayIP.size() > 0) {

                                          while (threadNumNow >= MAXTHREADNUM) {

                                                 for (Thread thread : arrayThread) {

                                                        if (!thread.getState().equals(

                                                                      Thread.State.TERMINATED)) {

                                                               thread.join(5);

                                                        }

                                                        --threadNumNow;

                                                 }

                                                 arrayThread = new ArrayList<Thread>();

                                          }

                                          Thread thread = new Thread(new InnerClass(arrayIP

                                                        .remove(0)));

                                          thread.start();

                                          threadNumNow++;

                                          arrayThread.add(thread);

                                   }

                            } catch (Exception e) {

                                   e.printStackTrace();

                            }

                     }

              }



              private class InnerClass implements Runnable {

                     // 线程查询一个 IP 是否是可以连接的 是则加入到相应的 IP 数组

                     private String ip;



                     private InnerClass(String ip) {

                            this.ip = ip;

                     }



                     private boolean isUsedIPAddress(String ip) {

                            synchronized (this) {

                                   // 判断这个 IP 地址在当前局域网中是否是可连接的 IP

                                   Process process = null;

                                   BufferedReader bufReader = null;

                                   String bufReadLineString = null;

                                   try {

                                          process = Runtime.getRuntime().exec(

                                                        "ping " + ip + " -w 100 -n 1");

                                          bufReader = new BufferedReader(new InputStreamReader(

                                                        process.getInputStream()));

                                          for (int i = 0; i < 6 && bufReader != null; i++) {

                                                 bufReader.readLine();

                                          }

                                          bufReadLineString = bufReader.readLine();

                                          if (bufReadLineString == null) {

                                                 process.destroy();

                                                 return false;

                                          }

                                          if (bufReadLineString.indexOf("timed out") > 0

                                                        || bufReadLineString.length() < 17

                                                        || bufReadLineString.indexOf("invalid") > 0) {

                                                 process.destroy();

                                                 return false;

                                          }

                                   } catch (IOException e) {

                                          e.printStackTrace();

                                   }

                                   process.destroy();

                                   return true;

                            }

                     }



                     public void run() {

                            synchronized (this) {

                                   if (isUsedIPAddress(ip)) {

                                          arrayIPUsed.add(ip);

                                   }

                            }

                     }

              }

       }

}

方案二 :

方案二与方案一只有一点是不一样的,就是在与下面这个相关的代码,采用的是队列而不是数组

将 private volatile ArrayList<Thread> arrayThread;// 子线程段

改为: private volatile ArrayBlockingQueue<Thread> queueThread;// 子线程队列

当然,其他相关的地方的处理也要做些修改.

注!!!!

在 linux下运行发现InetAddress.getLocalHost().getHostAddress();的返回值是127.0.0.1,这个与 windows下的返回值不一样。由于jdk在win和linux下这个方法的差异,这个程序在linux下不能如预期的那样工作,需要做一些技术上的改进。

你可能感兴趣的:(java,jdk,thread,linux,.net)