服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

扫描整个网段的多线程程序


  扫描500个ip10秒钟左右, 一个ip等待0.3秒

用java写的过程编程,供参考:

package steeven;

import java.sql.*;

import java.io.*;

import java.util.*;

import java.text.*;

import javax.servlet.http.*;

public class ip extends common implements runnable{

public string ip; // ip, 用户名, 主机名

resultset list; // 分页显示的记录集

public ip cur; // 分页显示的当前记录

static public hashtable ping = new hashtable(); //ping 后的结果集

static int threadcount = 0; //当前线程的数量, 防止过多线程摧毁电脑

public ip() {}

public ip(string ip){

this.ip=ip;

thread r = new thread(this);

r.start();

}

public static void ping(string ip) throws exception{

//最多30个线程

while(threadcount>30)

thread.sleep(50);

threadcount +=1;

ip p = new ip(ip);

}

public void pingall() throws exception{

threadcount =0;

ping = new hashtable();

while(next()) //next()对所有局域网ip放到cur

ping(cur.ip);

//等着所有ping结束

while(threadcount>0)

thread.sleep(50);

}

public void run(){

try{

process p= runtime.getruntime().exec ("ping "+ip+ " -w 300 -n 1");

inputstreamreader ir = new inputstreamreader(p.getinputstream());

linenumberreader input = new linenumberreader (ir);

//读取结果行

for (int i=1 ; i<7; i++)

input.readline();

string line= input.readline();

if (line.length()<17 || line.substring(8,17).equals("timed out"))

ping.put(ip,new boolean(false));

else

ping.put(ip,new boolean(true));

//线程结束

threadcount -= 1;

}catch (ioexception e){}

}

public static void main(string[] args) throws exception{

ip ip= new ip();

ip.pingall();

java.util.enumeration key = ping.keys();

string k;

while((k = (string)key.nextelement()) != null)

system.out.println(k+": "+ping.get(k));

}

}

扫描关注微信公众号