《精品资料(2021-2022年收藏)计算机网络课程设计端口扫描实验报告.doc》由会员分享,可在线阅读,更多相关《精品资料(2021-2022年收藏)计算机网络课程设计端口扫描实验报告.doc(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、目录1 需求分析.31.1 课程设计目的.31.2 课程设计内容.31.3 课程设计要求.32 概要设计.32.1 原理概述.32.2 运行环境.32.3 基本设计思路.32.4 功能模块设计.33 详细设计.33.1 程序流程.33.2 主要算法.53.3 主要数据结构.63.4 主要函数说明.64 用户使用手册.75 项目分析与总结.75.1 项目分析.75.2 总结与建议.8附录 A源程序代码文件说明.8附录 B参考文献.2021.需求分析1.1 课程设计目的加深对 TCP/IP 的理解,熟悉 socket 编程。课程设计内容实现一个扫描器,使用 TCP connect 进行端口扫描,并
2、把扫描到的结果记录下来。课程设计要求(1)Windows 或 Linux 环境下,程序在单机上运行;(2)使用端口扫描对一台主机进行扫描,并显示出结果;对一个网段进行IP 扫描,显示出结果;(3)提供友好的用户界面。2.概要设计2.1 原理概述TCP connect 扫描是最基本的扫描,操作系统提供的 connect()系统调用,用来与每一个感兴趣的目标计算机的端口进行连接。如果端口处于侦听状态,那么 connect()就能成功。否则,这个端口是不能用的,即没有提供服务。2.2 运行环境Windows xp,VC+6.02.3 基本设计思路创建一个 CSocket 套接字,通过 CSocket
3、 的 Connect 函数测试该主机的某个端口是否能够连通,获得该端口的打开状态。2.4 功能模块设计(1)测试主机某个端口是否打开;(2)“扫描结果”标题栏;(3)扫描单个或多个端口的单选按钮;(4)“扫描”,“停止”,“保存”按钮。3.详细设计3.1 程序流程3开始扫描初始化扫描单个端口?Tm_bSinglePort=true;m_cSinglePort.EnableWindow();m_cPortFrom.EnableWindow(false);m_cPortTo.EnableWindow(false);m_cBtnStop.EnableWindow(false);Fm_bSingleP
4、ort=false;m_cSinglePort.EnableWindow(false);m_cPortFrom.EnableWindow();m_cPortTo.EnableWindow();m_cBtnStop.EnableWindow(false);设置端口开始侦听端口有错误?显示结果FT设置下一端口扫描结束43.2 主要算法(1)测试主机某个端口是否打开的函数 TestConnection():BOOL CMyDlg:TestConnection(CString IP,UINT nPort)CSocket*pSocket;pSocket=new CSocket;ASSERT(pSocke
5、t);if(!pSocket-Create()delete pSocket;pSocket=NULL;return false;while(!pSocket-Connect(IP,nPort)delete pSocket;pSocket=NULL;return false;pSocket-Close();delete pSocket;return true;(2)响应单选按钮“扫描单个端口”和“扫描多个端口”的单击消息:void CMyDlg:OnRadio1Single()m_bSinglePort=true;m_cSinglePort.EnableWindow();m_cPortFrom.
6、EnableWindow(false);m_cPortTo.EnableWindow(false);m_cBtnStop.EnableWindow(false);void CMyDlg:OnRadio2Range()m_bSinglePort=false;m_cSinglePort.EnableWindow(false);m_cPortFrom.EnableWindow();m_cPortTo.EnableWindow();m_cBtnStop.EnableWindow(false);(3)显示列表框标题栏的成员函数:/增加列表框标题栏的某一列BOOL CMyDlg:AddColumn(LPC
7、TSTR strItem,int nItem,int nSubItem,int nMask,int nFmt)LV_COLUMN lvc;lvc.mask=nMask;lvc.fmt=nFmt;lvc.pszText=(LPTSTR)strItem;lvc.cx=m_cResult.GetStringWidth(lvc.pszText)+25;if(nMask&LVCF_SUBITEM)5if(nSubItem!=-1)lvc.iSubItem=nSubItem;elselvc.iSubItem=nItem;return m_cResult.InsertColumn(nItem,&lvc);/
8、在列表框中加一条BOOL CMyDlg:AddItem(int nItem,int nSubItem,LPCTSTR strItem,int nImageIndex)LV_ITEM lvItem;lvItem.mask=LVIF_TEXT;lvItem.iItem=nItem;lvItem.iSubItem=nSubItem;lvItem.pszText=(LPTSTR)strItem;if(nImageIndex!=-1)lvItem.mask|=LVIF_IMAGE;lvItem.iImage|=LVIF_IMAGE;if(nSubItem=0)return m_cResult.Inser
9、tItem(&lvItem);return m_cResult.SetItem(&lvItem);/加一列标题栏字符void CMyDlg:AddHeader(LPTSTR hdr)if(m_pColumns)m_pColumns-AddTail(hdr);/显示列表框标题栏void CMyDlg:ShowHeaders()int nIndex=0;POSITION pos=m_pColumns-GetHeadPosition();while(pos)CString hdr=(CString)m_pColumns-GetNext(pos);AddColumn(hdr,nIndex+);(4)“
10、扫描”,“停止”,“保存”按钮,见附录程序。3.3 主要数据结构及主要函数说明(1)测试主机某个端口是否打开TestConnection 函数;(2)“扫描结果”标题栏成员函数AddHeader,AddColumn,ShowHeaders;在列表框中添加字符串的函数AddItem;(3)扫 描 单 个 或 多 个 端 口 的 单 选 按 钮 OnRadioSingle()和OnRadioRange();(4)“扫描”,“停止”,“保存”按钮OnButton1Start(),OnButton2Stop(),6OnButton3Save();4.用户使用手册输入要扫描的主机 IP 地址,选择“扫描
11、单个端口”或“扫描多个端口”,若选择前者,则输入单个端口号;若选择后者,则输入端口范围。再输入扫描次数(默认值为 1),单击“扫描”按钮,扫描结果列表框中将会显示所输入端口的扫描结果。单击“停止”时停在那一刻的状态,并显示已扫描的结果。单击“保存”时,可将结果保存在文本文件中。5.项目分析与总结5.1 项目分析扫描单个端口的结果:扫描多个端口:7通过观察,主机 IP 为 10.5.104.36 的 25 号端口是打开的。5.2 总结与建议通过这次实验,加深了对端口的理解,也对 socket 编程有了进一步的认识。实验中遇到许多函数和定义需要上网查阅资料,而且调试的过程也比较困难,需要向同学请教
12、。所以,今后在编程和查阅资料方面的能力有待于进一步去提高。附录 A源程序代码文件说明:(1)端口扫描 Dlg.h:#if!defined(AFX_DLG_H_37F5060C_6D8B_42F0_90D9_FBA754BAD52B_INCLUDED_)#define AFX_DLG_H_37F5060C_6D8B_42F0_90D9_FBA754BAD52B_INCLUDED_#if _MSC_VER 1000#pragma once#endif/_MSC_VER 1000#include#include#include 8#include#include/扫描结果typedef struct
13、int nAttempts;TCHAR IPAddress16;TCHAR port5;BOOL bStatus;/1=open,0=closeDATA;/CMyDlg dialogclass CMyDlg:public CDialog/Constructionpublic:CMyDlg(CWnd*pParent=NULL);/standard constructor/Dialog Data/AFX_DATA(CMyDlg)enum IDD=IDD_MY_DIALOG;CProgressCtrlm_cProgress;CListCtrlm_cResult;CIPAddressCtrlm_cIP
14、;CEditm_cAttempts;CEditm_cPortTo;CEditm_cPortFrom;CEditm_cSinglePort;CButton m_cBtnStop;CButton m_cBtnScan;/AFX_DATA/ClassWizard generated virtual function overrides/AFX_VIRTUAL(CMyDlg)protected:virtual void DoDataExchange(CDataExchange*pDX);/DDX/DDV support/AFX_VIRTUAL/Implementationprotected:HICON
15、 m_hIcon;BOOL TestConnection(CString IP,UINT nPort);/测试主机某个端口是否打开void ShowHeaders(void);/显示列表框标题栏void AddHeader(LPTSTR hdr);/增加一列标题栏字符AddItem(intnItem,intnSubItem,LPCTSTRstrItem,intnImageIndex=-1);/向m_cResult 输出一个结果/向输出结构列表控件增加一列9BOOL AddColumn(LPCTSTR strItem,int nItem,int nSubItem=-1,int nMask=LVC
16、F_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM,int nFmt=LVCFMT_LEFT);/变量UINT m_nMaxAttempts;/试图连接次数的最大值BOOL m_bSinglePort;/是否只扫描单个端口UINT m_minPort,m_maxPort;/扫描端口的范围UINT m_nCounter;/端口的个数CStringList*m_pColumns;/列表框标题栏CPtrList*m_pStatusList;/保存扫描结果的链表/Generated message map functions/AFX_MSG(CMyDlg)virtual B
17、OOL OnInitDialog();afx_msg void OnSysCommand(UINT nID,LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButton1Start();afx_msg void OnButton2Stop();afx_msg void OnButton3Save();afx_msg void OnRadio1Single();afx_msg void OnRadio2Range();/AFX_MSGDECLARE_MESSAGE_MAP
18、();/AFX_INSERT_LOCATION/Microsoft Visual C+will insert additional declarations immediately before theprevious line.#endif/!defined(AFX_DLG_H_37F5060C_6D8B_42F0_90D9_FBA754BAD52B_INCLUDED_)(2)端口扫描 Dlg.cpp:#includestdafx.h#include端口扫描.h#include端口扫描 Dlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FI
19、LEstatic char THIS_FILE=_FILE_;#endif/CAboutDlg dialog used for App Aboutclass CAboutDlg:public CDialog public:CAboutDlg();/Dialog Data10/AFX_DATA(CAboutDlg)enum IDD=IDD_ABOUTBOX;/AFX_DATA/ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CD
20、ataExchange*pDX);/DDX/DDV support/AFX_VIRTUAL/Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg():CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange*pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDl
21、g)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg,CDialog)/AFX_MSG_MAP(CAboutDlg)/No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/CMyDlg dialogCMyDlg:CMyDlg(CWnd*pParent/*=NULL*/):CDialog(CMyDlg:IDD,pParent)/在对话框的构造函数中初始化成员变量/AFX_DATA_INIT(CMyDlg)/AFX_DATA_INIT/Note that LoadIcon does not require a subseq
22、uent DestroyIcon in Win32m_hIcon=AfxGetApp()-LoadIcon(IDR_MAINFRAME);m_pColumns=new CStringList;ASSERT(m_pColumns);m_bSinglePort=true;m_nMaxAttempts=1;m_pStatusList=new CPtrList;11ASSERT(m_pStatusList);void CMyDlg:DoDataExchange(CDataExchange*pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMyDlg)DDX_
23、Control(pDX,IDC_PROGRESS1,m_cProgress);DDX_Control(pDX,IDC_LIST1,m_cResult);DDX_Control(pDX,IDC_IPADDRESS1,m_cIP);DDX_Control(pDX,IDC_EDIT4_ATTEMPTS,m_cAttempts);DDX_Control(pDX,IDC_EDIT3_SINGLE_PORT_TO,m_cPortTo);DDX_Control(pDX,IDC_EDIT2_SINGLE_PORT_FROM,m_cPortFrom);DDX_Control(pDX,IDC_EDIT1_SING
24、LE_PORT,m_cSinglePort);DDX_Control(pDX,IDC_BUTTON2_STOP,m_cBtnStop);DDX_Control(pDX,IDC_BUTTON1_START,m_cBtnScan);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMyDlg,CDialog)/AFX_MSG_MAP(CMyDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1_START,OnButton1Start)ON_BN_CLICKED(IDC_BUT
25、TON2_STOP,OnButton2Stop)ON_BN_CLICKED(IDC_BUTTON3_SAVE,OnButton3Save)ON_BN_CLICKED(IDC_RADIO1_SINGLE,OnRadio1Single)ON_BN_CLICKED(IDC_RADIO2_RANGE,OnRadio2Range)/AFX_MSG_MAPEND_MESSAGE_MAP()/CMyDlg message handlersBOOL CMyDlg:OnInitDialog()/设置按钮的初始状态和列表框的风格CDialog:OnInitDialog();/Add About.menu item
26、 to system menu./IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX&0 xFFF0)=IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);/Set the icon for this dialog.The framework does this automatically/when the applications
27、 main window is not a dialogSetIcon(m_hIcon,TRUE);/Set big iconSetIcon(m_hIcon,FALSE);/Set small icon/TODO:Add extra initialization here/给列表框加标题栏AddHeader(_T(IP 地址);/加一列标题栏字符AddHeader(_T(端口号);AddHeader(_T(端口状态);AddHeader(_T(尝试连接次数);AddHeader(_T(标记);ShowHeaders();/设置扫描单个端口CheckRadioButton(IDC_RADIO1_
28、SINGLE,IDC_RADIO2_RANGE,IDC_RADIO1_SINGLE);/设置控件的默认状态m_cSinglePort.EnableWindow();m_cPortFrom.EnableWindow(false);m_cPortTo.EnableWindow(false);m_cBtnStop.EnableWindow(false);m_cAttempts.SetWindowText(_T(1);return TRUE;/return TRUEunless you set the focus to a controlvoid CMyDlg:OnSysCommand(UINT nI
29、D,LPARAM lParam)if(nID&0 xFFF0)=IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID,lParam);/If you add a minimize button to your dialog,you will need the code below/to draw the icon.For MFC applications using the document/view model,/this is automatically done for you by
30、 the framework.void CMyDlg:OnPaint()if(IsIconic()13CPaintDC dc(this);/device context for paintingSendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);/Center icon in client rectangleint cxIcon=GetSystemMetrics(SM_CXICON);int cyIcon=GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int
31、x=(rect.Width()-cxIcon+1)/2;int y=(rect.Height()-cyIcon+1)/2;/Draw the icondc.DrawIcon(x,y,m_hIcon);elseCDialog:OnPaint();/The system calls this to obtain the cursor to display while the user drags/the minimized window.HCURSOR CMyDlg:OnQueryDragIcon()return(HCURSOR)m_hIcon;void CMyDlg:OnButton1Start
32、()/“扫描”按钮/TODO:Add your control notification handler code hereCString btnTxt,IP;UINT nSinglePort;BYTE f1,f2,f3,f4;TCHAR temp10=0;m_cProgress.SetPos(0);m_cResult.DeleteAllItems();POSITION p=m_pStatusList-GetHeadPosition();while(p)POSITION temp=p;DATA*pNode=(DATA*)m_pStatusList-GetNext(p);m_pStatusLis
33、t-RemoveAt(temp);if(pNode)/pNode!=nulldelete pNode;if(m_cIP.IsBlank()MessageBox(请输入主机 IP 地址);return;14if(m_cIP.GetAddress(f1,f2,f3,f4)4)MessageBox(请输入有效的 IP 地址);return;IP=_itoa(f1,temp,10);IP+=_T(.);IP+=_itoa(f2,temp,10);IP+=_T(.);IP+=_itoa(f3,temp,10);IP+=_T(.);IP+=_itoa(f4,temp,10);m_cBtnStop.Enab
34、leWindow();m_cBtnScan.EnableWindow(false);if(m_bSinglePort)CString port;m_cSinglePort.GetWindowText(port);m_minPort=m_maxPort=nSinglePort=atoi(port);elseCString port1,port2;m_cPortFrom.GetWindowText(port1);m_cPortTo.GetWindowText(port2);m_minPort=atoi(port1);m_maxPort=atoi(port2);m_cProgress.SetRang
35、e32(0,m_maxPort-m_minPort+1);m_cProgress.SetStep(1);if(!m_bSinglePort&m_maxPortm_minPort)MessageBox(_T(您输入的端口范围有误,请重新输入);return;UINT m_nMaxAttempts=GetDlgItemInt(IDC_EDIT4_ATTEMPTS);for(m_nCounter=m_minPort;m_nCounter=m_maxPort;m_nCounter+)BOOL bIsOpen=false;UINT nAttempt=1;while(nAttemptSetWindowTe
36、xt(str);str.Empty();bIsOpen=TestConnection(IP,m_nCounter);if(bIsOpen)DATA*pNode=new DATA;ASSERT(pNode);strcpy(pNode-IPAddress,IP.GetBuffer(IP.GetLength();strcpy(pNode-port,_itoa(m_nCounter,temp,10);pNode-bStatus=1;/openpNode-nAttempts=nAttempt;m_pStatusList-AddTail(pNode);nAttempt+;if(!bIsOpen)DATA*
37、pNode=new DATA;ASSERT(pNode);strcpy(pNode-IPAddress,IP.GetBuffer(IP.GetLength();strcpy(pNode-port,_itoa(m_nCounter,temp,10);pNode-bStatus=0;/closepNode-nAttempts=nAttempt-1;m_pStatusList-AddTail(pNode);MSG message;if(:PeekMessage(&message,NULL,0,0,PM_REMOVE):TranslateMessage(&message);:DispatchMessa
38、ge(&message);m_cProgress.StepIt();CStatic*pStatic=(CStatic*)GetDlgItem(IDC_STATIC_STATUS);pStatic-SetWindowText(CString)_T(当前扫描状态);m_cBtnScan.EnableWindow();m_cBtnStop.EnableWindow(false);UINT nIndex=0;POSITION pos=m_pStatusList-GetHeadPosition();16while(pos)DATA*pNode=(DATA*)m_pStatusList-GetNext(p
39、os);AddItem(nIndex,0,pNode-IPAddress);AddItem(nIndex,1,pNode-port);if(pNode-bStatus)AddItem(nIndex,2,_T(Open);AddItem(nIndex,4,_T(*);elseAddItem(nIndex,2,_T(Close);AddItem(nIndex,4,_T();AddItem(nIndex+,3,_itoa(pNode-nAttempts,temp,10);void CMyDlg:OnButton2Stop()/“停止”按钮/TODO:Add your control notifica
40、tion handler code herem_nCounter=m_maxPort+1;m_cBtnStop.EnableWindow(false);m_cBtnScan.EnableWindow();CStatic*pStatic=(CStatic*)GetDlgItem(IDC_STATIC_STATUS);pStatic-SetWindowText(CString)_T(当前扫描状态);void CMyDlg:OnButton3Save()/“保存”按钮/TODO:Add your control notification handler code hereCFileDialog*pD
41、lg=new CFileDialog(FALSE,_T(txt),NULL,OFN_OVERWRITEPROMPT|OFN_EXPLORER|OFN_LONGNAMES,_T(Scanned ports files(*.txt),this);ASSERT(pDlg);if(pDlg-DoModal()=IDOK)int nHandle,retVal;nHandle=_open(pDlg-GetPathName(),_O_BINARY|_O_CREAT|_O_TRUNC|_O_RDWR);if(nHandle=-1)MessageBox(_T(Unable to open output file
42、 to write.),_T(Error),MB_OK|MB_ICONEXCLAMATION);delete pDlg;return;POSITION pos=m_pStatusList-GetHeadPosition();17while(pos)CString buffer;DATA*pNode=(DATA*)m_pStatusList-GetNext(pos);buffer=pNode-IPAddress;buffer+=_T(,);buffer+=pNode-port;buffer+=_T(,);if(pNode-bStatus)buffer+=_T(Open);elsebuffer+=
43、_T(Close);buffer+=_T(rn0);retVal=_write(nHandle,(void*)buffer.GetBuffer(buffer.GetLength(),buffer.GetLength();if(retVal!=buffer.GetLength()MessageBox(_T(An error occured while writing records.),_T(Error),MB_OK|MB_ICONEXCLAMATION);delete pDlg;return;buffer.Empty();_close(nHandle);delete pDlg;void CMy
44、Dlg:OnRadio1Single()/TODO:Add your control notification handler code herem_bSinglePort=true;m_cSinglePort.EnableWindow();m_cPortFrom.EnableWindow(false);m_cPortTo.EnableWindow(false);m_cBtnStop.EnableWindow(false);void CMyDlg:OnRadio2Range()/TODO:Add your control notification handler code herem_bSin
45、glePort=false;m_cSinglePort.EnableWindow(false);m_cPortFrom.EnableWindow();m_cPortTo.EnableWindow();m_cBtnStop.EnableWindow(false);18/测试主机的某个端口是否打开BOOL CMyDlg:TestConnection(CString IP,UINT nPort)CSocket*pSocket;pSocket=new CSocket;ASSERT(pSocket);if(!pSocket-Create()delete pSocket;pSocket=NULL;retu
46、rn false;while(!pSocket-Connect(IP,nPort)delete pSocket;pSocket=NULL;return false;pSocket-Close();delete pSocket;return true;/增加列表框标题栏的某一列BOOL CMyDlg:AddColumn(LPCTSTR strItem,int nItem,int nSubItem,int nMask,int nFmt)LV_COLUMN lvc;lvc.mask=nMask;lvc.fmt=nFmt;lvc.pszText=(LPTSTR)strItem;lvc.cx=m_cRe
47、sult.GetStringWidth(lvc.pszText)+25;if(nMask&LVCF_SUBITEM)if(nSubItem!=-1)lvc.iSubItem=nSubItem;elselvc.iSubItem=nItem;return m_cResult.InsertColumn(nItem,&lvc);/在列表框中加一条BOOL CMyDlg:AddItem(int nItem,int nSubItem,LPCTSTR strItem,int nImageIndex)LV_ITEM lvItem;lvItem.mask=LVIF_TEXT;lvItem.iItem=nItem
48、;19lvItem.iSubItem=nSubItem;lvItem.pszText=(LPTSTR)strItem;if(nImageIndex!=-1)lvItem.mask|=LVIF_IMAGE;lvItem.iImage|=LVIF_IMAGE;if(nSubItem=0)return m_cResult.InsertItem(&lvItem);return m_cResult.SetItem(&lvItem);/加一列标题栏字符void CMyDlg:AddHeader(LPTSTR hdr)if(m_pColumns)m_pColumns-AddTail(hdr);/显示列表框标题栏void CMyDlg:ShowHeaders()int nIndex=0;POSITION pos=m_pColumns-GetHeadPosition();while(pos)CString hdr=(CString)m_pColumns-GetNext(pos);AddColumn(hdr,nIndex+);附录 B参考文献:Visual C+实效编程百例(第二版)人民邮电出版社