try{
theTCPsocket=newSocket(hostAddress,i);
theTCPsocket.close();
ThreadScan.Result.append(""+i);
switch(i){
case21:
porttype="(FTP)";
break;
case23:
porttype="(TELNET)";
break;
case25:
porttype="(SMTP)";
break;
case80:
porttype="(HTTP)";
break;
case110:
porttype="(POP)";
break;
case139:
porttype="(netBIOS)";
break;
case1433:
porttype="(SQLServer)";
break;
case3389:
porttype="(TerminalService)";
break;
case443:
porttype="(HTTPS)";
break;
case1521:
porttype="(Oracle)";
break;
}
//端口没有特定类别
if(porttype.equals("0")){
ThreadScan.Result.append("\n");
}
else{
ThreadScan.Result.append(":
"+porttype+"\n");
}
}
catch(IOExceptione){
}
}
//扫描完成后,显示扫描完成,并将【确定】按钮设置为可用
if(i==MAX_port+Integer.parseInt(ThreadScan.maxThread.getText())){
ThreadScan.Result.append("\n"+"扫描完成...");
//将【确定】按钮设置成为可用
if(!
ThreadScan.Submit.isEnabled()){
ThreadScan.Submit.setEnabled(true);
}
}
}
}
}
ThreadScan的代码
import.*;
importjava.io.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
/*
*实现扫描的主体程序
*/
publicclassThreadScan{
publicstaticJFramemain=newJFrame("JAVA端口扫描器");
//显示扫描结果
publicstaticJTextAreaResult=newJTextArea("",4,40);
//滚动条面板
publicstaticJScrollPaneresultPane=newJScrollPane(Result,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//输入主机名文本框
publicstaticJTextFieldhostname=newJTextField("localhost",8);
//输入ip地址前3位的输入框
publicstaticJTextFieldfromip1=newJTextField("0",3);
//输入ip地址4~6位的输入框
publicstaticJTextFieldfromip2=newJTextField("0",3);
//输入ip地址7~9位的输入框
publicstaticJTextFieldfromip3=newJTextField("0",3);
//输入起始ip地址最后4位的输入框
publicstaticJTextFieldfromip4=newJTextField("0",3);
//输入目标ip地址最后4位的输入框
publicstaticJTextFieldtoip=newJTextField("0",3);
//输入最小端口的输入框
publicstaticJTextFieldminPort=newJTextField("0",4);
//输入最大端口的输入框
publicstaticJTextFieldmaxPort=newJTextField("1000",4);
//输入最大线程数量的输入框
publicstaticJTextFieldmaxThread=newJTextField("100",3);
//错误提示框
publicstaticJDialogDLGError=newJDialog(main,"错误!
");
publicstaticJLabelDLGINFO=newJLabel("");
publicstaticJLabeltype=newJLabel("请选择:
");
//扫描类型
publicstaticJRadioButtonradioIp=newJRadioButton("IP地址:
");
publicstaticJRadioButtonradioHost=newJRadioButton("主机名:
",true);
//单选框组
publicstaticButtonGroupgroup=newButtonGroup();
publicstaticJLabelP1=newJLabel("端口范围:
");
publicstaticJLabelP2=newJLabel("~");
publicstaticJLabelP3=newJLabel("~");
publicstaticJLabelPdot1=newJLabel(".");
publicstaticJLabelPdot2=newJLabel(".");
publicstaticJLabelPdot3=newJLabel(".");
publicstaticJLabelTNUM=newJLabel("线程数:
");
publicstaticJLabelRST=newJLabel("扫描结果:
");
publicstaticJLabelcon=newJLabel("");
//定义按钮
publicstaticJButtonOK=newJButton("确定");
publicstaticJButtonSubmit=newJButton("开始扫描");
publicstaticJButtonCancel=newJButton("退出");
publicstaticJButtonsaveButton=newJButton("保存扫描结果");
//菜单栏
publicstaticJMenuBarmyBar=newJMenuBar();
publicstaticJMenumyMenu=newJMenu("文件(F)");
publicstaticJMenuItemsaveItem=newJMenuItem("保存扫描结果(S)");
publicstaticJMenuItemexitItem=newJMenuItem("退出(Q)");
publicstaticJMenumyMenu2=newJMenu("帮助");
publicstaticJMenuItemhelpItem=newJMenuItem("阅读");
publicstaticvoidmain(String[]args){
main.setSize(500,400);
main.setLocation(300,300);
main.setResizable(false);
main.setLayout(newGridBagLayout());
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DLGError.setSize(300,100);
DLGError.setLocation(400,400);
//添加“菜单栏”
myMenu.add(saveItem);
myMenu.add(exitItem);
myMenu2.add(helpItem);
myBar.add(myMenu);
myBar.add(myMenu2);
main.setJMenuBar(myBar);
//设置热键
myMenu.setMnemonic('F');
saveItem.setMnemonic('S');
//为“另存为”组件设置快捷键为ctrl+s
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
exitItem.setMnemonic('Q');
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
//采用表格包型布局
ContainermPanel=main.getContentPane();
GridBagConstraintsc=newGridBagConstraints();
c.insets=newInsets(10,0,0,10);
c.gridx=0;
c.gridy=0;
c.gridwidth=10;
c.fill=GridBagConstraints.BOTH;
c.anchor=GridBagConstraints.CENTER;
mPanel.add(type,c);
group.add(radioIp);
group.add(radioHost);
c.gridx=0;
c.gridy=1;
c.gridwidth=1;
c.fill=GridBagConstraints.BOTH;
c.anchor=GridBagC