- 论坛徽章:
- 0
|
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
//java版扫描器实现
/**
* @author 黎声
*
*/
public class Scan extends JFrame implements ActionListener{
JLabel jl1 = new JLabel("IP");
JLabel jl2 = new JLabel("端口范围");
JLabel jl3 = new JLabel("-");
JLabel jl4 = new JLabel("线程数");
JTextField jtf1 = new JTextField(10);
JTextField jtf2 = new JTextField(4);
JTextField jtf3 = new JTextField(4);
JTextField jtf4 = new JTextField(3);
JButton jb = new JButton("开始扫描");
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JTextArea jta = new JTextArea();
//定义线程组对象,方便对多个线程进行管理
ThreadGroup tg = new ThreadGroup("g1");
public Scan()
{
jtf1.setText("127.0.0.1");
jtf2.setText("0");
jtf3.setText("10000");
jtf4.setText("100");
jta.setEditable(false);
jp1.add(jl1);
jp1.add(jtf1);
jp1.add(jl2);
jp1.add(jtf2);
jp1.add(jl3);
jp1.add(jtf3);
jp1.add(jl4);
jp1.add(jtf4);
jp2.add(jb);
this.add(jp1,BorderLayout.NORTH);
this.add(new JScrollPane(jta));
this.add(jp2,BorderLayout.SOUTH);
jb.addActionListener(this);
this.setTitle("扫描器");
this.setResizable(false);
this.setSize(500,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(jb.getText().equals("开始扫描"))
{
jb.setText("停止扫描");
jta.setText("");
SmRunnable.flag = true;
//启动线程
int num = Integer.parseInt(jtf4.getText());
for(int i=0;inum;i++)
{
Thread t = new Thread(tg,new SmRunnable(i,this));
t.start();
}
Check t1 = new Check();
t1.start();
}
else
{
//点击停止扫描按钮
jb.setText("开始扫描");
SmRunnable.flag = false;
jta.append("用户取消操作! ");
}
}
//检查所有用户线程是否结束线程类
class Check extends Thread
{
public void run()
{
while(true)
{
if(tg.activeCount()==0)
{
jb.setText("开始扫描");
jta.append("扫描完毕!");
return;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Scan scan = new Scan();
}
}
//扫描线程类
class SmRunnable implements Runnable
{
int threadno;//线程号
static int threadnum; //线程数
static Scan scan;
static boolean flag = true;
public SmRunnable(int threadno,Scan scan)
{
this.threadno = threadno;
SmRunnable.scan = scan;
}
public void run() {
int min = Integer.parseInt(scan.jtf2.getText());
int max = Integer.parseInt(scan.jtf3.getText());
int threadnum = Integer.parseInt(scan.jtf4.getText());
for(int i=min+threadno;imax;i+=threadnum)
{
try {
Socket socket = new Socket(scan.jtf1.getText(),i);
scan.jta.append("ip:"+scan.jtf1.getText()+" 端口"+i+"开放\n");
}catch (Exception e) {
//e.printStackTrace();
}
if(flag==false) break; //停止线程
}
}
}
![]()
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/62780/showart_493199.html |
|