AD

simple java program thread scan LAN ip

import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
import java**.InetAddress;   
import java**.UnknownHostException;   
import java.util.ArrayList;   
import java.util.Iterator;   
  
/**  
 *   Scan local area network using the java thread  ip  Simple solution    
 * @author Administrator  
 *  
 */  
public class LanIP {   
       
    public static void main(String[] args) {   
        System.out.println("  Start ......");   
        LanIP ip = new LanIP();   
        ArrayList<String> list =  ip.getLanIPArrayList();   
        System.out.println("  Finally, the number of  ===>"+list.size());   
    }   
       
    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;//   Check the IP address of the last one starting point     
  
        private int lastIP = 255;//   Check the IP address of the last end point     
  
        private volatile ArrayList<Thread> arrayThread;//   Child thread segment     
  
        private final int MAXTHREADNUM = 30; //   The maximum number of threads simultaneously sub-     
  
        private int threadNumNow;//   The number of ongoing sub-thread     
  
        private volatile ArrayList<String> arrayIP;//   LAN IP address of all possible query result sets     
  
        private volatile ArrayList<String> arrayIPUsed;//   LAN IP address already in use query result set     
  
        private InitSystem(String ip) {   
            System.out.println("IP===>"+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 {   
                System.out.println("getArrayIPUsed:  arrayIP.size===>"+arrayIP.size());   
                while (arrayIP.size() > 0) {   
                    Thread.sleep(300);   
                }   
            } catch (InterruptedException e) {   
                e.printStackTrace();   
            }   
            return arrayIPUsed;   
        }   
  
        private void setIPAddressList(String ip) {   
            //   According to this ip address lookup in the LAN it all possible   IP   Address set     
            int lastPointIndex = ip.lastIndexOf('.');   
            String stringIPHead = ip.substring(0, ++lastPointIndex);   
            System.out.println("stringIPHead===>"+stringIPHead);   
            String stringIP = null;   
            for (int i = firstIP; i <= lastIP; i++) {   
                stringIP = stringIPHead + i;   
                arrayIP.add(stringIP);   
            }   
            System.out.println("  Enter into here ... arrayIP the total number of  :"+arrayIP.size());   
        }   
  
        public void run() {   
            synchronized (this) {   
                try {   
                    System.out.println("run()  arrayIP.size()===>"+arrayIP.size());   
                    System.out.println("run()  threadNumNow===>"+threadNumNow);   
                    System.out.println("arrayThread.size()"+arrayThread.size());   
                    while (arrayIP.size() > 0) {   
                        while (threadNumNow >= MAXTHREADNUM) {   
                            System.out.println("  Threads beyond the 30, stay behind  ...");   
                            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 {   
            //   Check whether a thread can be connected to IP     Is then added to the corresponding IP array     
            private String ip;   
  
            private InnerClass(String ip) {   
                System.out.println("InnerClass ip===>"+ip);   
                this.ip = ip;   
            }   
  
            private boolean isUsedIPAddress(String ip) {   
                System.out.println("isUsedIPAddress===>"+ip);   
                synchronized (this) {   
                    System.out.println("  Enter here  .....");   
                    //  To determine the IP address if the current LAN connection is available   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();   
                        System.out.println("bufReadLineString===>"+bufReadLineString);   
                        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);   
                    }   
                }   
            }   
        }   
    }   
}   

  Access to the LAN IP address using the name of the computer  .mac  Address. Working Group  

System.out.println("192.168.1.187  MAC is the corresponding card  :");

NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName("192.168.1.187"));

byte[]mac=ne.getHardwareAddress();

String mac_s=hexByte(mac[0])+":"+hexByte(mac[1])+":"+ hexByte(mac[2])+":"+hexByte(mac[3])+":"+ hexByte(mac[4])+":"+hexByte(mac[5]);System.out.println(mac_s);

  Result of the program  : 192.168.1.187  MAC is the corresponding card  : 00:0c:f1:20:75:58 
标签: lt, import java, java util, string args, ioexception, implements, main string, private class, arraylist, java program, iterator, bufferedreader, inputstreamreader, program thread, list ip
分类: Java
时间: 2010-12-20

相关文章

  1. Write a simple Java program file directory monitor

    Recent bet nothing, a simple to use Java to write a version of the file directory monitoring program to monito ...
  2. The Java program into Windows Service

    I. Introduction Java language programmers are more and more attention. And Java-related applications are more ...
  3. Java multi-threading: Thread Introduction

    First, the thread overview Running thread is the basic execution units. When the operating system (not includi ...
  4. java.lang.Thread. notes.

    From: http://www.ismayday.com/?p=133 Multithreading is such a mechanism, which allows multiple concurrent exec ...
  5. Simple proxy program development process (1)

    Abstract: A simple proxy program development process. This example is mainly the use of a number of programmin ...
  6. In the java program using comm port in the local management [Back Qinbo: Senior Software Engineer]

    In the java program using comm port in the local management Recently did telecom project encountered a number ...
  7. java program to run linux

    Recently made a timing module, the program's implementation is probably as follows, each packed into a regular ...
  8. JDBC - make java program connected to the database

    Recent learn a database, how to program in java to call their own database has become an urgent need to solve ...
  9. Java program to achieve through SMSLib library to send text messages

    Java program to achieve through SMSLib library to send text messages SMS (Short Messaging Service) or Short Me ...
  10. ORACLE JAVA program in the next issue of the development

    / / Developed under ORACLE JAVA program in question Talk about JAVA programs developed under ORACLE problem So ...
  11. How effectively prevent the Java program source code was peeping?

    Java program's source code is easy for people to peep, as long as an anti-compiler, any person can analyze oth ...
  12. JAVA program services will be made of the method WINDOWS

    JAVA program services will be made of the method WINDOWS A lot of time necessary to process on WINDOWS SERVER ...
  13. Use Java Service Wrapper to Java program as Windows system service

    Transfer from: http://www.cjsdn.net/post/view?bid=21&id=95116&sty=1&tpg=3&age=0 In many cases ...
  14. Import the Eclipse Java program

    Import the Eclipse Java program to import Eclipse Java program, sub-Import Java Project and Java source code i ...
  15. java program to run a detailed analysis of mechanisms

    JVM (Java Virtual Machine) was used to compute the standard equipment can be used in different ways (software ...
  16. Java program in the "memory leak" problem

    Transfer from: http://blog.csdn.net/gaofei_upc/archive/2009/12/17/5026951.aspx Soar Abstract: Java's memory ma ...
  17. C # method calls within a Java program

    . Net generation later than the java, the library package java in some ways better than, more comprehensive. F ...
  18. Title: a person can step, one can register a. Two or three steps, and assume that n-class level, please write a java program will print out all the moves.

    Title: a person can step, one can board one, two or three steps, and assume that n-class level, please write a ...
  19. The second java program - drawing board

    I. Features: 1. To achieve draw lines, circles, rectangles, four rubber shapes. Drawing mode: hold down the mo ...