用VC++6.0实现网络监听的程序.doc_第1页
用VC++6.0实现网络监听的程序.doc_第2页
用VC++6.0实现网络监听的程序.doc_第3页
用VC++6.0实现网络监听的程序.doc_第4页
用VC++6.0实现网络监听的程序.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/NO_DEPENDENCIES/ Microsoft Developer Studio generated include file./ Used by SniffApp.rc/#define IDM_ABOUTBOX 0x0010#define IDD_ABOUTBOX 100#define IDS_ABOUTBOX 101#define IDD_SNIFFAPP_DIALOG 102#define IDR_MAINFRAME 128#define IDI_ICON1 129#define IDC_LIST1 1000#define IDC_BEGINLISTEN 1001#define IDC_CANCEL 1002/ Next default values for new objects/ #ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE 130#define _APS_NEXT_COMMAND_VALUE 32771#define _APS_NEXT_CONTROL_VALUE 1003#define _APS_NEXT_SYMED_VALUE 101#endif#endif下一部分/ SniffApp.cpp : Defines the class behaviors for the application./#include stdafx.h#include SniffApp.h#include SniffAppDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CSniffAppAppBEGIN_MESSAGE_MAP(CSniffAppApp, CWinApp)/AFX_MSG_MAP(CSniffAppApp)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code!/AFX_MSGON_COMMAND(ID_HELP, CWinApp:OnHelp)END_MESSAGE_MAP()/ CSniffAppApp constructionCSniffAppApp:CSniffAppApp()/ TODO: add construction code here,/ Place all significant initialization in InitInstance/ The one and only CSniffAppApp objectCSniffAppApp theApp;/ CSniffAppApp initializationBOOL CSniffAppApp:InitInstance()AfxEnableControlContainer();/ Standard initialization/ If you are not using these features and wish to reduce the size/ of your final executable, you should remove from the following/ the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls();/ Call this when using MFC in a shared DLL#elseEnable3dControlsStatic();/ Call this when linking to MFC statically#endif/初始化套接字WSADATA data;AfxSocketInit(&data);/WSAStartup(2,&data);CSniffAppDlg dlg;m_pMainWnd = &dlg;int nResponse = dlg.DoModal();if (nResponse = IDOK)/ TODO: Place code here to handle when the dialog is/ dismissed with OKelse if (nResponse = IDCANCEL)/ TODO: Place code here to handle when the dialog is/ dismissed with Cancel/ Since the dialog has been closed, return FALSE so that we exit the/ application, rather than start the applications message pump.return FALSE;再下一篇/ SniffApp.h : main header file for the SNIFFAPP application/#if !defined(AFX_SNIFFAPP_H_2D147315_8DFA_4AD1_9CA1_7C609AB716B0_INCLUDED_)#define AFX_SNIFFAPP_H_2D147315_8DFA_4AD1_9CA1_7C609AB716B0_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#ifndef _AFXWIN_H_#error include stdafx.h before including this file for PCH#endif#include resource.h/ main symbols/ CSniffAppApp:/ See SniffApp.cpp for the implementation of this class/#include winsock2.h#pragma comment (lib,ws2_32.lib)#include AFXSOCK.Hclass CSniffAppApp : public CWinApppublic:CSniffAppApp();/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CSniffAppApp)public:virtual BOOL InitInstance();/AFX_VIRTUAL/ Implementation/AFX_MSG(CSniffAppApp)/ NOTE - the ClassWizard will add and remove member functions here./ DO NOT EDIT what you see in these blocks of generated code !/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_SNIFFAPP_H_2D147315_8DFA_4AD1_9CA1_7C609AB716B0_INCLUDED_)下下/ SniffAppDlg.cpp : implementation file/#include stdafx.h#include SniffApp.h#include SniffAppDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* 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(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CSniffAppDlg dialogPROTONAME protos11 = IPPROTO_IP ,IP,IPPROTO_ICMP,ICMP,IPPROTO_IGMP,IGMP,IPPROTO_GGP,GGP,IPPROTO_TCP,TCP,IPPROTO_PUP,PUP,IPPROTO_UDP,UDP,IPPROTO_IDP,IDP,IPPROTO_ND,ND,IPPROTO_RAW,RAW,IPPROTO_MAX,MAX;char* get_protoname(int protoID)for (int i= 0; iLoadIcon(IDR_MAINFRAME);m_pThread = NULL;void CSniffAppDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CSniffAppDlg)DDX_Control(pDX, IDC_LIST1, m_List);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CSniffAppDlg, CDialog)/AFX_MSG_MAP(CSniffAppDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BEGINLISTEN, OnBeginlisten)ON_BN_CLICKED(IDC_CANCEL, OnCancel)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CSniffAppDlg message handlersBOOL CSniffAppDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = 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 main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small iconm_List.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_GRIDLINES|LVS_EX_TWOCLICKACTIVATE);m_List.InsertColumn(0,协议,LVCFMT_LEFT,80);m_List.InsertColumn(1,源IP地址,LVCFMT_LEFT,120);m_List.InsertColumn(2,目的IP地址,LVCFMT_LEFT,120);m_List.InsertColumn(3,通信端口,LVCFMT_LEFT,80);m_List.InsertColumn(4,大小,LVCFMT_LEFT,100);m_List.InsertColumn(5,数据,LVCFMT_LEFT,120);return TRUE; / return TRUE unless you set the focus to a controlvoid CSniffAppDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = 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 the framework.void CSniffAppDlg:OnPaint() if (IsIconic()CPaintDC 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 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 CSniffAppDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CSniffAppDlg:OnOK() /线程函数UINT ThreadFun( LPVOID pParam )CSniffAppDlg* pDlg = static_cast(pParam);MSG msg;char buffer1000,sourceip32 ,*tempbuf;char *ptemp;BYTE* pData = NULL; /实际数据报中的数据UINT sourceport ;CString str;HEADIP* pHeadIP;HEADICMP* pHeadICMP;HEADUDP* pHeadUDP;HEADTCP* pHeadTCP;in_addr addr;int ret;while (TRUE)pData = NULL;if (PeekMessage(&msg,pDlg-m_hWnd,WM_CLOSE,WM_CLOSE,PM_NOREMOVE )closesocket(pDlg-m_Sock);break;memset(buffer,0,1000);ret = recv(pDlg-m_Sock,buffer,1000,0);if (ret = SOCKET_ERROR)continue;else /接收到数据tempbuf = buffer;pHeadIP = (HEADIP*)tempbuf;/获取数据报总长度WORD len = ntohs(pHeadIP-totallen);/获取源IPpDlg-m_List.InsertItem(pDlg-m_List.GetItemCount(),);addr.S_un.S_addr = pHeadIP-sourceIP;ptemp = inet_ntoa(addr);pDlg-m_List.SetItemText(pDlg-m_List.GetItemCount()-1,1,ptemp);/获取目的IPaddr.S_un.S_addr = pHeadIP-destIP;ptemp = inet_ntoa(addr);pDlg-m_List.SetItemText(pDlg-m_List.GetItemCount()-1,2,ptemp);/获取协议名称ptemp = get_protoname(pHeadIP-proto);strcpy(sourceip,ptemp);pDlg-m_List.SetItemText(pDlg-m_List.GetItemCount()-1,0,sourceip);/获取IP数据报总长度WORD ipSumLen = ntohs(pHeadIP-totallen);/IP数据报头总长度int ipHeadLen = 20;/获得去除IP层数据的长度WORD netlen = ipSumLen - ipHeadLen;/根据不同大协议获得不同协议的数据switch (pHeadIP-proto)case IPPROTO_ICMP:pHeadICMP = (HEADICMP*)(tempbuf+20); pData = (BYTE*)(pHeadICMP)+4; /ICMP数据报头共4个字节/获取数据的长度netlen -= 4;break;case IPPROTO_UDP:pHeadUDP = (HEADUDP*)(tempbuf+20); pData = (BYTE*)pHeadUDP+8; /UDP数据报头共8个字节sourceport = ntohs(pHeadUDP-SourcePort);str.Format(%d,sourceport);/设置源端口pDlg-m_List.SetItemText(pDlg-m_List.GetItemCount()-1,3,str);str.Empty();netlen -= 8;break;case IPPROTO_TCP:pHeadTCP = (HEADTCP*)(tempbuf+20);sourceport = ntohs(pHeadTCP-SourcePort);pData = (BYTE*)pHeadTCP+20; /TCP数据报头共20个字节 str.Format(%d,sourceport);/设置源端口pDlg-m_List.SetItemText(pDlg-m_List.GetItemCount()-1,3,str);str.Empty();netlen-= 20;break;/设置数据大小str.Format(%d,netlen);pDlg-m_List.SetItemText(pDlg-m_List.GetItemCount()-1,4,str);str.Empty();/设置数据if (pData != NULL)str.Format( %s,pData);pDlg-m_List.SetItemText(pDlg-m_List.GetItemCount()-1,5,str);str.Empty();return 0;void CSniffAppDlg:OnBeginlisten() /创建套接字m_Sock = socket(AF_INET,SOCK_RAW, IPPROTO_IP );char name128;memset(name,0,128);hostent* phostent;phostent = gethostbyname(name);DWORD ip;ip = inet_addr(inet_ntoa(*(in_addr*)phostent-h_addr_list0);int timeout = 4000; /超时4秒/设置接收数据的超时时间setsockopt(m_Sock,SOL_SOCKET,SO_RCVTIMEO,(const char*)&timeout,sizeof(timeout);sockaddr_in skaddr;skaddr.sin_family = AF_INET;skaddr.sin_port = htons(700);skaddr.sin_addr.S_un.S_addr = ip;/绑定地址if ( bind(m_Sock,(sockaddr*)&skaddr,sizeof(skaddr)=SOCKET_ERROR)MessageBox(地址绑定错误);return;DWORD inBuffer=1;DWORD outBuffer10;DWORD reValue = 0;if (WSAIoctl(m_Sock,SIO_RCVALL,&inBuffer,sizeof(inBuffer),&outBuffer,sizeof(outBuffer),&reValue,NULL,NULL)=SOCKET_ERROR)MessageBox(设置缓冲区错误.);closesocket(m_Sock);return;elsem_pThread = AfxBeginThread(ThreadFun,(void*)this);void CSniffAppDlg:OnCancel()if (m_pThread)/m_pThread-ExitInstance();delete m_pThread;closesocket( m_Sock) ;CD

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论