import java.net.*;
import java.io.*;
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(Scan.maxThread.getText()))
{
Scan.status.setText("状态:扫描 "+i+" 端口");
try
{
theTCPsocket=new Socket(hostAddress,i);
theTCPsocket.close();
Scan.Result.append(" "+i);
switch(i)
{
case 21:
Scan.Result.append("(FTP)");
break;
case 22:
Scan.Result.append("(SSH)");
break;
case 23:
Scan.Result.append("(TELNET)");
break;
case 25:
Scan.Result.append("(SMTP)");
break;
case 80:
Scan.Result.append("(HTTP)");
break;
case 110:
Scan.Result.append("(POP)");
break;
case 139:
Scan.Result.append("(netBIOS)");
break;
case 443:
Scan.Result.append("(HTTPS)");
break;
case 1433:
Scan.Result.append("(MSSQL)");
break;
case 3306:
Scan.Result.append("(MYSQL)");
break;
case 3389:
Scan.Result.append("(Terminal Services)");
break;
case 4000:
Scan.Result.append("(Tencent QQ)");
break;
}
Scan.Result.append(",");
}
catch (IOException e)
{}
}
if (i==MAX_port)
{
Scan.Result.append("\n"+"Completed ...");
Scan.status.setText("状态:Completed!");
}
}
}
class Cancle implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(1);
}
}
class Submit implements ActionListener
{
public void actionPerformed (ActionEvent a)
{
int minPort;
int maxPort;
int maxThread;
try
{
minPort=Integer.parseInt(Scan.minPort.getText());
maxPort=Integer.parseInt(Scan.maxPort.getText());
maxThread=Integer.parseInt(Scan.maxThread.getText());
}
catch(NumberFormatException e)
{
Scan.DLGINFO.setText("端口号或线程数错误!端口号和线程数必须为整数!");
Scan.DLGError.setVisible(true);
Scan.status.setText("状态:No start ");
return;
}
try
{
TCPThread.hostAddress=InetAddress.getByName(Scan.hostname.getText());
}
catch(UnknownHostException e)
{
Scan.DLGINFO.setText(" IP/域名或地址错误! ");
Scan.DLGError.setVisible(true);
Scan.status.setText("状态:No start ");
return;
}
if(minPort<0 || minPort>65535 || minPort>maxPort)
{
Scan.DLGINFO.setText("端口界于0-65535 !");
Scan.DLGError.setVisible(true);
return;
}
else TCPThread.MIN_port=minPort;
if(maxPort<0 || maxPort>65535 || maxPort<minPort)
{
Scan.DLGINFO.setText("端口界于0-65535 !");
Scan.DLGError.setVisible(true);
Scan.status.setText("状态:No start");
return;
}
else TCPThread.MAX_port=maxPort;
if(maxThread<1 || maxThread>300)
{
Scan.DLGINFO.setText(" 线程数为1-300的整数! ");
Scan.DLGError.setVisible(true);
Scan.status.setText("状态:No start");
return;
}
Scan.Result.setText("");
Scan.Result.append("正在扫描 "+Scan.hostname.getText()+" 线程数 "+Scan.maxThread.getText()+"\n");
Scan.Result.append("开放端口: ");
for(int i=0;i<maxThread;i++)
{
new TCPThread("T" + i,i).start();
}
}
}
class OK implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
Scan.DLGError.dispose();
}
}
public class Scan
{
public static JFrame main=new JFrame("JAVA的多线程TCP端口扫描程序 ");
public static JTextArea Result=new JTextArea("",4,43);
public static JTextField hostname=new JTextField("www.eviloctal.com",10);
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,"Wrong!");
public static Label DLGINFO=new Label("");
public static Label status=new Label("状态:No start");
public static void main(String[] args)
{
main.setSize(500,220);
main.setLocation(320,320);
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("扫描结果: Onlyer[EST-VIP]");
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 Submit());
Cancel.addActionListener(new Cancle());
OK.addActionListener(new OK());
}
}