捷凌网安JAVA频道
捷凌IT教育
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME 开源技术 其他技术 论坛
 您现在的位置: 捷凌网安 >> Java >> 高级技术 >> 多线程 >> 正文
扫描整个网段的多线程程序

作者:佚名 责任编辑:左决 点击数: 更新时间:2008-3-24 9:35:33

扫描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));

}

}
  • 上一篇文章:

  • 下一篇文章:
  •  
     最进更新
    普通文章J2SE实现windows读取网卡的物05-11
    普通文章Scala—Java的避难所之main(05-11
    普通文章Spring数据源的灵活配置巧应05-11
    普通文章Spring 与 Log4J 进行动态日05-11
    普通文章Heritrix的多线程ToeThread和05-11
    普通文章应用spring示例开发网站构思05-11
    普通文章Spring 数据源配置与应用05-11
    普通文章Spring中的四种声明式事务的05-11
    普通文章Hibernate+Spring搞定Clob、05-11
    普通文章Hibernate的映射关联关系05-11
     
     推荐文章
    推荐文章Java技术开源搜索引擎04-30
    推荐文章Eclipse中建立自己的JUnit测04-30
    推荐文章Eclipse 3.3上安装jadclipse04-30
    推荐文章spring 编程入门十大问题解答04-30
    推荐文章Java编程中Spring的一些负面04-30
    推荐文章Java应用中Hibernate对多表关04-30
    推荐文章关于hibernate的缓存和CRUD04-30
    推荐文章基于Struts1.2的动态多文件上04-30
    推荐文章演示Struts2实现简单上传代码04-30
    推荐文章J2EE学习笔记--Struts初步认04-30
     
     热点文章
    普通文章J2SE实现windows读取网卡的物05-11
    普通文章Scala—Java的避难所之main(05-11
    普通文章Spring数据源的灵活配置巧应05-11
    普通文章Spring 与 Log4J 进行动态日05-11
    普通文章Heritrix的多线程ToeThread和05-11
    普通文章应用spring示例开发网站构思05-11
    普通文章Spring 数据源配置与应用05-11
    普通文章Spring中的四种声明式事务的05-11
    普通文章Hibernate+Spring搞定Clob、05-11
    普通文章Hibernate的映射关联关系05-11

    | 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 友情链接 | 版权申明 | 网站地图 |

    在线交流 捷凌网安主群:51649627
    Copyright 2007-2008 © 捷凌网安. All rights reserved.
    备案序号:蜀ICP备08001812号