import java.net.*;
import java.io.*;
//import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TCPThread extends Thread
{
public static InetAddress hostAddress;
public static int MIN_port;
public static int MAX_port;
private int threadnum;
public TCPThread(String name,int threadnum)
{
super(name);
this.threadnum = threadnum;
}
public void run()
{
int i;
Socket theTCPsocket;
for (i = MIN_port+threadnum; i < MAX_port; i += Integer.parseInt(ThreadScan.maxThread.getText()))
{
ThreadScan.status.setText("扫描状态:正在扫描 "+i+" 端口");
try
{
theTCPsocket=new Socket(hostAddress,i);
theTCPsocket.close();
ThreadScan.Result.append(" "+i);
switch(i)
{
case 21:
ThreadScan.Result.append("(FTP)");
break;
case 23:
ThreadScan.Result.append("(TELNET)");
break;
case 25:
ThreadScan.Result.append("(SMTP)");
break;
case 80:
ThreadScan.Result.append("(HTTP)");
break;
case 110:
ThreadScan.Result.append("(POP)");
break;
case 139:
ThreadScan.Result.append("(netBIOS)");
break;
}
ThreadScan.Result.append(",");
}
catch (IOException e)
{}
}
if (i==MAX_port)
{
ThreadScan.Result.append("\n"+"扫描完成...");
ThreadScan.status.setText("扫描状态:完成!");
}
}
}
class CancleAction implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(1);
}
}
class SubmitAction implements ActionListener
{
public void actionPerformed (ActionEvent a)
{
int minPort;
int maxPort;
int maxThread;
try
{
minPort=Integer.parseInt(ThreadScan.minPort.getText());
maxPort=Integer.parseInt(ThreadScan.maxPort.getText());
maxThread=Integer.parseInt(ThreadScan.maxThread.getText());
}
catch(NumberFormatException e)
{
ThreadScan.DLGINFO.setText("错误的端口号或线程数!端口号和线程数必须为整数!");
ThreadScan.DLGError.setVisible(true);
ThreadScan.status.setText("扫描状态:未开始");
return;
}
try
{
TCPThread.hostAddress=InetAddress.getByName(ThreadScan.hostname.getText());
}
catch(UnknownHostException e)
{
ThreadScan.DLGINFO.setText(" 错误的IP地址/域名或地址不可达! ");
ThreadScan.DLGError.setVisible(true);
ThreadScan.status.setText("扫描状态:未开始");
return;
}
if(minPort<0 || minPort>65535 || minPort>maxPort)
{
ThreadScan.DLGINFO.setText("最小端口必须是0-65535并且小于最大端口的整数!");
ThreadScan.DLGError.setVisible(true);
return;
}
else TCPThread.MIN_port=minPort;
if(maxPort<0 || maxPort>65535 || maxPort<minPort)
{
ThreadScan.DLGINFO.setText("最大端口必须是0-65535并且大于最小端口的整数!");
ThreadScan.DLGError.setVisible(true);
ThreadScan.status.setText("扫描状态:未开始");
return;
}
else TCPThread.MAX_port=maxPort;
if(maxThread<1 || maxThread>200)
{
ThreadScan.DLGINFO.setText(" 线程数为1-200的整数! ");
ThreadScan.DLGError.setVisible(true);
ThreadScan.status.setText("扫描状态:未开始");
return;
}
ThreadScan.Result.setText("");
ThreadScan.Result.append("正在扫描 "+ThreadScan.hostname.getText()+" 线程数 "+ThreadScan.maxThread.getText()+"\n");
ThreadScan.Result.append("开放端口: ");
for(int i=0;i<maxThread;i++)
{
new TCPThread("T" + i,i).start();
}
}
}
class OKAction implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
ThreadScan.DLGError.dispose();
}
}
public class ThreadScan
{
public static JFrame main=new JFrame("基于JAVA的多线程TCP端口扫描程序");
public static JTextArea Result=new JTextArea("",4,43);
public static JTextField hostname=new JTextField("localhost",8);
public static JTextField minPort=new JTextField("0",4);
public static JTextField maxPort=new JTextField("1000",4);
public static JTextField maxThread=new JTextField("100",3);
public static JDialog DLGError=new JDialog(main,"错误!");
public static Label DLGINFO=new Label("");
public static Label status=new Label("扫描状态:未开始");
public static void main(String[] args)
{
main.setSize(500,220);
main.setLocation(300,300);
main.setResizable(false);
DLGError.setSize(300,100);
DLGError.setLocation(400,400);
JPanel mainPanel = new JPanel();
Label H=new Label("ip或域名:");
Label P1=new Label("端口范围:");
Label P2=new Label("~");
Label TNUM=new Label("线程数:");
Label RST=new Label("扫描结果: ");
Label con=new Label(" ");
Result.setLineWrap(true);
Result.setEditable(false);
JButton OK = new JButton("确定");
JButton Submit = new JButton("确定");
JButton Cancel = new JButton("取消");
Container mPanel = main.getContentPane();
Container dPanel = DLGError.getContentPane();
mPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
dPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
mPanel.add(H);
mPanel.add(hostname);
mPanel.add(P1);
mPanel.add(minPort);
mPanel.add(P2);
mPanel.add(maxPort);
mPanel.add(TNUM);
mPanel.add(maxThread);
mPanel.add(RST);
mPanel.add(Result);
mPanel.add(con);
mPanel.add(Submit);
mPanel.add(Cancel);
mPanel.add(status);
dPanel.add(DLGINFO);
dPanel.add(OK);
main.setVisible(true);
Submit.addActionListener(new SubmitAction());
Cancel.addActionListener(new CancleAction());
OK.addActionListener(new OKAction());
}
}