《端口扫描程序设计详细的报告材料源代码.doc》由会员分享,可在线阅读,更多相关《端口扫描程序设计详细的报告材料源代码.doc(20页珍藏版)》请在课桌文档上搜索。
1、word网络综合实验任 务 书一、 目的与要求1 性质:设计性实验。2 任务:设计并实现一个端口扫描程序,检测某个IP或某段IP的计算机的端口工作情况。3 目的:加深对课堂讲授知识的理解,熟练掌握基本的网络编程技术和方法,建立网络编程整体概念,使得学生初步具有研究、设计、编制和调试网络程序的能力。4 要求:熟悉有关定义、概念和实现算法,设计出程序流程框图和数据结构,编写出完整的源程序,基本功能完善,方便易用,操作无误。5 学生要求人数:1人。二、 主要容1 编写一个端口扫描程序,能够显示某个IP或某段IP的计算机的某一个或某些端口是否正在工作。基本工作过程如下:(1) 设定好一定的端口扫描围;
2、(2) 设定每个端口扫描的次数,因为有可能有的端口一次扫描可能不通;(3) 创建socket,通过socket的connect方法来连接远程IP地址以及对应的端口;(4) 如果返回false,表示端口没有开放,否则端口开放。三、 进度计划序号设计容完成时间备注1任务布置及资料收集第1周周一、周二2软件方案设计与制定第1周周三至周四3编程实现第1周周周五至第2周周二4程序测试与修改,撰写报告第2周周三至周五上午5报告提交,软件验收周五下午四、设计成果要求1 完成规定的实验任务,保质保量;2 完成综合实验报告,要求格式规,容具体而翔实,应体现自身所做的工作,注重对实验思路的归纳和对问题解决过程的总
3、结。五、 考核方式1 平时成绩验收答辩实验报告;2 五级分制。 学生: 指导教师: 一、综合实验的目的与要求1目的:加深对课堂讲授知识的理解,熟练掌握基本的网络编程技术和方法,建立网络编程整体概念,加深对JAVA编程语言的使用,促进编程能力的提高,同时为网络编程打下一个比较好的基础。同时对实验的过程要有完全的了解。2. 要求:熟悉有关定义、概念和实现算法,设计出程序流程框图和数据结构,编写出完整的源程序,基本功能完善,方便易用,操作无误。二、综合实验正文1编程语言的选择针对编写的是端口扫描的程序,所以我选择了JAVA编程,因为要简单而方便的试验端口扫描的功能。2端口扫描实现的功能是:显示某个I
4、P或某段IP的计算机的某一个或某些端口是否正在工作21 如何选择IP段和端口创建socket,通过socket的connect方法来连接远程IP地址以及对应的端口。而我使用的是socket函数来实现端口的扫描和IP地址的选择扫描。22 如何实现快速扫描端口通过实现多线程,当一个IP或者一个端口同时分配多个线程进行扫描,速度会比单线程扫描快许多。23 显示扫描结果可以在使用JAVA编写的时候,可以通过使用JScrollPane控件实现显示结果。24 实现要求功能之余美化外观3创建应用程序界面31 建立主窗体 ,“JAVA端口扫描器”图1-132 IP地址的输入部分图1-2 33 端口部分,其中包
5、括指定的端口和指定扫描的端口围图1-334 多线程的实现和延时的控制部分图1-435 增加的保存及快捷键功能图1-5 4扫描的整个过程41 填写IP地址 192.168.1.100192.168.1.12042 填写端口扫描的围为130043 线程数选择为10044 扫描结果如下图1-6三端口扫描的主要部分程序如下import .*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;/* *实现扫描的主体程序 */public class ThreadScanpublic static
6、 JFrame main=new JFrame(JAVA端口扫描器);/显示扫描结果public static JTextArea Result=new JTextArea(,4,40);/滚动条面板 public staticJScrollPaneresultPane=newJScrollPane(Result,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;/输入主机名文本框public static JTextField hostname=new JTextField(
7、localhost,8);/输入ip地址前3位的输入框public static JTextField fromip1=new JTextField(0,3);/输入ip地址46位的输入框public static JTextField fromip2=new JTextField(0,3);/输入ip地址79位的输入框public static JTextField fromip3=new JTextField(0,3);/输入起始ip地址最后4位的输入框public static JTextField fromip4=new JTextField(0,3);/输入目标ip地址最后4位的输入
8、框public static JTextField toip=new JTextField(0,3);/输入最小端口的输入框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
9、(main,错误!);public static JLabel DLGINFO=new JLabel();public static JLabel type=new JLabel(请选择:);/扫描类型public static JRadioButton radioIp = new JRadioButton(IP地址:);public static JRadioButton radioHost = new JRadioButton(主机名:,true);/单选框组public static ButtonGroup group = new ButtonGroup();public static
10、JLabel P1=new JLabel(端口围:);public static JLabel P2=new JLabel();public static JLabel P3=new JLabel();public static JLabel Pdot1 = new JLabel(.);public static JLabel Pdot2 = new JLabel(.);public static JLabel Pdot3 = new JLabel(.);public static JLabel TNUM=new JLabel(线程数:);public static JLabel RST=ne
11、w JLabel(扫描结果: );public static JLabel con=new JLabel( );/定义按钮public static utton OK = new utton(确定);public static utton Submit = new utton(开始扫描);public static utton Cancel = new utton(退出);public static utton saveButton = new utton(保存扫描结果);/菜单栏public static JMenuBar myBar = new JMenuBar();public stat
12、ic JMenu myMenu = new JMenu(文件(F);public static JMenuItem saveItem = new JMenuItem(保存扫描结果(S);public static JMenuItem exitItem = new JMenuItem(退出(Q);public static void main(String args)main.setSize(500,400);main.setLocation(300,300);main.setResizable(false);main.setLayout(new GridBagLayout();main.set
13、DefaultCloseOperation(JFrame.EXIT_ON_CLOSE);DLGError.setSize(300,100);DLGError.setLocation(400,400);/添加“菜单栏”myMenu.add(saveItem);myMenu.add(exitItem);myBar.add(myMenu);main.setJMenuBar(myBar);/设置热键myMenu.setMnemonic(F); saveItem.setMnemonic (S); /为“另存为”组件设置快捷键为ctrl+ssaveItem.setAccelerator(KeyStroke
14、.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK); exitItem.setMnemonic(Q); exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK); /采用表格包型布局Container mPanel = main.getContentPane();GridBagConstraints c = new GridBagConstraints();c.insets = new Insets(10,0,0,10);c.gridx =
15、 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 = GridBagConstraints.CENTER;mPanel.add(radioIp,c);c.gridx
16、 = 1;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(fromip1,c);c.gridx = 2;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(Pdot1,c);c.gridx = 3;c.gridy = 1;c.gridwidth = 1;c.fil
17、l = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(fromip2,c);c.gridx = 4;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(Pdot2,c);c.gridx = 5;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor =
18、 GridBagConstraints.CENTER;mPanel.add(fromip3,c);c.gridx = 6;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(Pdot3,c);c.gridx = 7;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(
19、fromip4,c);c.gridx = 8;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(P2,c);c.gridx = 9;c.gridy = 1;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(toip,c);c.gridx = 0;c.gridy = 2;c.gridwid
20、th = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(radioHost,c);c.gridx = 1;c.gridy = 2;c.gridwidth = 3;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(hostname,c);c.gridx = 0;c.gridy = 3;c.gridwidth = 1;c.fill = GridBagConstraint
21、s.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(P1,c);c.gridx = 1;c.gridy = 3;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(minPort,c);c.gridx = 2;c.gridy = 3;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENT
22、ER;mPanel.add(P3,c);c.gridx = 3;c.gridy = 3;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(maxPort,c);c.gridx = 0;c.gridy = 4;c.gridwidth = 1;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(TNUM,c);c.gridx = 1;c.gridy
23、 = 4;c.gridwidth = 3;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(maxThread,c);c.gridx = 0;c.gridy = 5;c.gridwidth = 3;c.fill = GridBagConstraints.VERTICAL;c.anchor = GridBagConstraints.CENTER;mPanel.add(Submit,c);c.gridx = 6;c.gridy = 5;c.gridwidth = 4;c.fill = G
24、ridBagConstraints.VERTICAL;c.anchor = GridBagConstraints.CENTER;mPanel.add(Cancel,c);c.gridx = 0;c.gridy = 6;c.gridwidth = 10;c.fill = GridBagConstraints.BOTH;c.anchor = GridBagConstraints.CENTER;mPanel.add(RST,c);/设置文本区域可以换行Result.setLineWrap(true);/设置文本区域不可编辑Result.setEditable(false);c.gridx = 0;c
25、.gridy = 7;c.gridwidth = 10;c.gridheight = 4;c.fill = GridBagConstraints.VERTICAL;c.anchor = GridBagConstraints.CENTER;mPanel.add(resultPane,c);Container dPanel = DLGError.getContentPane();dPanel.setLayout(new FlowLayout(FlowLayout.CENTER);dPanel.add(DLGINFO);dPanel.add(OK);Submit.addActionListener(
26、new SubmitAction();Cancel.addActionListener(new CancleAction();OK.addActionListener(new OKAction();/实现退出功能exitItem.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) System.exit(0););/实现帮助功能saveButton.addActionListener(new java.awt.event.A
27、ctionListener() public void actionPerformed(java.awt.event.ActionEvent e) JFileChooser fc=new JFileChooser();int returnVal=fc.showSaveDialog(null);/点击“保存”if(returnVal = 0)File saveFile=fc.getSelectedFile();try FileWriter writeOut = new FileWriter(saveFile);writeOut.write(ThreadScan.Result.getText();
28、writeOut.close();catch (IOException ex) System.out.println(保存失败);/点击“取消”elsereturn;);main.setVisible(true);/* *实现“取消”功能 *退出程序 */class CancleAction implements ActionListenerpublic void actionPerformed (ActionEvent e)System.exit(0);/* *实现“确定”功能 *完成扫描 */class SubmitAction implements ActionListenerpubli
29、c void actionPerformed (ActionEvent a)int minPort;int maxPort;int maxThread;int ip1 = 0;int ip2 = 0;int ip3 = 0;int ipstart = 0;int ipend = 0;String ipaddress = ;String hostname = ;ThreadScan.Result.setText();/将确定按钮设置成为不可用if(ThreadScan.Submit.isEnabled()ThreadScan.Submit.setEnabled(false);/* *判断搜索的类
30、型 *按照ip地址扫描:type = 0 *按照主机名称扫描:type = 1 */if(ThreadScan.radioIp.isSelected()TCPThread.type = 0;/判断ip的前3位是否为int型tryip1=Integer.parseInt(ThreadScan.fromip1.getText();catch(NumberFormatException e)ThreadScan.DLGINFO.setText(错误的ip!);ThreadScan.DLGError.setVisible(true);return;/判断ip的46位是否为int型tryip2=Inte
31、ger.parseInt(ThreadScan.fromip2.getText();catch(NumberFormatException e)ThreadScan.DLGINFO.setText(错误的ip!);ThreadScan.DLGError.setVisible(true);return;/判断ip的79位是否为int型tryip3=Integer.parseInt(ThreadScan.fromip3.getText();catch(NumberFormatException e)ThreadScan.DLGINFO.setText(错误的ip!);ThreadScan.DLGE
32、rror.setVisible(true);return;/判断起始ip的最后4位是否为int型tryipstart=Integer.parseInt(ThreadScan.fromip4.getText();catch(NumberFormatException e)ThreadScan.DLGINFO.setText(错误的ip!);ThreadScan.DLGError.setVisible(true);return;/判断目标ip的最后4位是否为int型tryipend=Integer.parseInt(ThreadScan.toip.getText();catch(NumberFor
33、matException e)ThreadScan.DLGINFO.setText(错误的目标ip!);ThreadScan.DLGError.setVisible(true);return;/判断起始ip是否正确/判断条件:大于0且小于等于255if(ip1255|ip2255|ip3255|ipstart255)ThreadScan.DLGINFO.setText( ip地址为0-255的整数! );ThreadScan.DLGError.setVisible(true);return;elseTCPThread.ip1 = ip1;TCPThread.ip2 = ip2;TCPThrea
34、d.ip3 = ip3;TCPThread.ipstart = ipstart;/判断目标ip是否正确/判断条件:大于0且小于等于255if(ipend255)ThreadScan.DLGINFO.setText(目标ip地址为0-255的整数! );ThreadScan.DLGError.setVisible(true);return;elseTCPThread.ipend = ipend;ipaddress = + ip1 + ip2 + ip3 + ipstart;/* *判断ip地址的有效性 */tryTCPThread.hostAddress=InetAddress.getByNam
35、e(ipaddress);catch(UnknownHostException e)ThreadScan.DLGINFO.setText( 错误的IP或地址不可达! );ThreadScan.DLGError.setVisible(true);return;/根据主机名进行端口扫描if(ThreadScan.radioHost.isSelected()TCPThread.type = 1;/* *判断主机名称的有效性 */tryTCPThread.hostAddress=InetAddress.getByName(ThreadScan.hostname.getText();catch(Unkn
36、ownHostException e)ThreadScan.DLGINFO.setText(错误的域名或地址不可达! );ThreadScan.DLGError.setVisible(true);return;/* *判断端口号的有效性 */tryminPort=Integer.parseInt(ThreadScan.minPort.getText();maxPort=Integer.parseInt(ThreadScan.maxPort.getText();maxThread=Integer.parseInt(ThreadScan.maxThread.getText();catch(Numb
37、erFormatException e)ThreadScan.DLGINFO.setText(错误的端口号或线程数!端口号和线程数必须为整数!);ThreadScan.DLGError.setVisible(true);return;/* *判断最小端口号的有效围 *判断条件:大于0且小于65535,最大端口应大于最小端口 */if(minPort65535 | minPortmaxPort)ThreadScan.DLGINFO.setText(最小端口必须是0-65535并且小于最大端口的整数!);ThreadScan.DLGError.setVisible(true);return;els
38、eTCPThread.MIN_port=minPort;/* *判断最大端口号的有效围 *判断条件:大于0且小于65535,最大端口应大于最小端口 */if(maxPort65535 | maxPortminPort)ThreadScan.DLGINFO.setText(最大端口必须是0-65535并且大于最小端口的整数!);ThreadScan.DLGError.setVisible(true);return;elseTCPThread.MAX_port=maxPort;/* *判断线程数量的有效围 *判断条件:大于1且小于200 */if(maxThread200)ThreadScan.D
39、LGINFO.setText(线程数为1-200的整数! );ThreadScan.DLGError.setVisible(true);return;ThreadScan.Result.append(线程数 +ThreadScan.maxThread.getText()+n);/启动线程for(int i=0;imaxThread;i+)new TCPThread(T + i,i).start();/* *实现错误提示框中的“确定”按钮功能 */class OKAction implements ActionListenerpublic void actionPerformed (Action
40、Event e)ThreadScan.DLGError.dispose();多线程类文件:import .*;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TCPThread extends Threadpublic static InetAddress hostAddress;/最小的端口号public static int MIN_port;/最大的端口号public static int MAX_port;/线程总数private int threadnum; /查询方式:0为ip;1为主机名public static