




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、局域网屏幕监控 下面是程序执行结果: 程序中使用从 CSocket 继承来的 CTCPClientSocket 和 CTCPServerSocket 来相互通信。并由客户端使用 OnStart( ) 建立连接,通信地址选用回环地址 127.0.0.1 来进行测试,如果要监控特定主机,须输入该机 ip 地址: void CScreenDlg:OnStart() clientsocket.Create(); run = false; bool ret = clientsocket.Connect(127.0.0.1,1033); /连接到1033端口 if (!ret) return; this-
2、SetTimer(0,1000,NULL); /设置时间间隔是1秒 服务器端进行监听: BOOL CServerDlg:OnInitDialog() CDialog:OnInitDialog(); / Add About. menu item to system menu. / IDM_ABOUTBOX must be in the system command range. ASSERT(IDM_ABOUTBOX ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR); pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX,
3、 strAboutMenu); / Set the icon for this dialog. The framework does this automatically / when the applications main window is not a dialog SetIcon(m_hIcon, TRUE);/ Set big icon SetIcon(m_hIcon, FALSE);/ Set small icon / TODO: Add extra initialization here bool ret = false; serversocket.SetServerEvent
4、(this); ret = serversocket.Create(1033); /打开1033端口 ret = serversocket.Listen(); / 进行监听 return TRUE; / return TRUE unless you set the focus to a control 建立连接后,服务器端使用GetScreen获取数据: 并转换成 2 进制信息: void CServerDlg:GetScreen() CDC dc,bmpdc; int width,height; dc.CreateDC(DISPLAY,NULL,NULL,NULL); CBitmap bm;
5、 width = GetSystemMetrics(SM_CXSCREEN); height = GetSystemMetrics(SM_CYSCREEN); bm.CreateCompatibleBitmap( bmpdc.CreateCompatibleDC( bmpdc.SelectObject( bmpdc.BitBlt(0,0,width,height, bm.GetBitmap( size = bitmap.bmWidthBytes * bitmap.bmHeight; bmpdata = new charsize; BITMAPINFOHEADER bih; bih.biBitC
6、ount=bitmap.bmBitsPixel; bih.biClrImportant=0; bih.biClrUsed=0; bih.biCompression=0; bih.biHeight=bitmap.bmHeight; bih.biPlanes=1; bih.biSize=sizeof(BITMAPINFOHEADER); bih.biSizeImage=size; bih.biWidth=bitmap.bmWidth; bih.biXPelsPerMeter=0; bih.biYPelsPerMeter=0; GetDIBits(dc,bm,0,bih.biHeight,bmpda
7、ta,(BITMAPINFO*) 发送图片数据: void CServerDlg:SendBitData(CSocket *socket) char *data = bmpdata; int sendlen=0; int len = 0; do /发送图像数据 sendlen = socket-Send(data,size); len += sendlen; data += sendlen; while(lenSend( 客户端使用 GetScreen( ) 来接送二进制数据: void CScreenDlg:GetScreen() char *buffer = M; / 获取图像结构信息 c
8、lientsocket.Send(buffer,strlen(buffer); clientsocket.Receive( size = bitmap.bmWidthBytes * bitmap.bmHeight; bmpdata = new charsize; /处理图像的动态数组 char * data = bmpdata; int len,receivelen; len = receivelen = 0; buffer = D; /获取图像数据 clientsocket.Send(buffer,strlen(buffer); do receivelen = clientsocket.Re
9、ceive(data,size); len += receivelen; data += receivelen; while(lenStretchBlt(0,0,rect.Width(),rect.Height(), 当关闭窗口时会调用 OnStopf 来做处理: void CScreenDlg:OnStop() this-KillTimer(0); /结束计时 clientsocket.ShutDown(2); clientsocket.Close(); / 关闭套接字 另外,继承自 CSocket 的 CTCPClentSocket 和 CTCPServerSocket 分别定义了 用于接
10、收的 OnAccept( ) 用于发送的 OnRecieve( )用于关闭套接字的 OnClose( ); OnAccept: void CTCPServerSocket:OnAccept(int nErrorCode ) CSocket:OnAccept(nErrorCode); clientsocket = new CTCPClientSocket(this); this-Accept(*clientsocket); POSITION pos = ClientList.GetHeadPosition(); ClientList.InsertBefore(pos,clientsocket);
11、 if (serverevent != NULL) clientsocket-SetClientEvent(serverevent); if (serverevent != NULL) serverevent-OnAccept(this,nErrorCode); OnRecieve: void CTCPClientSocket:OnReceive(int nErrorCode ) CSocket:OnReceive(nErrorCode); if (clientevent != NULL) clientevent-OnReceive(this,nErrorCode); OnClose: voi
12、d CTCPClientSocket:OnClose(int nErrorCode ) CSocket:OnClose(nErrorCode); if (clientevent != NULL) clientevent-OnClose(this,nErrorCode); POSITION pos = serversocket-ClientList.GetHeadPosition(); POSITION oldpos; while (pos != NULL) oldpos = pos; CTCPClientSocket *socket = (CTCPClientSocket*)serversoc
13、ket-ClientList.GetNext(pos); if (socket != NULL) serversocket-ClientList.RemoveAt(oldpos); 课程设计总结: 经过的了一周的学习和实践,加深了对网络程序设计的理解和应用。理解和 掌握了网络套接字的用法,使用规则。在编程上拓宽了自己的思路,学会了一 些灵活的方法,熟练了编程技巧。这次的课程设计中不仅检验了我所学习的知 识,也培养了我如何去把握一件事情,如何去做一件事情,又如何完成一件事 情。在设计过程中,与同学分工设计,和同学们相互探讨,相互学习,相互监 督。对于如何参考文献有了更深的理解。 参考资料: WI
14、NDOWS 网络与通信设计 王艳平 VISUAL C+ 范例完全自学手册 附源代码: Screen 工程中:Resourse.h /NO_DEPENDENCIES / Microsoft Developer Studio generated include file. / Used by Screen.rc / #define IDM_ABOUTBOX creen 0 x0010 #define IDD_ABOUTBOX 100 #define IDS_ABOUTBOX 101 #define IDD_SCREEN_DIALOG 102 #define IDR_MAINFRAME 128 #d
15、efine IDC_BUTTON1 1000 #define IDC_BUTTON2 1001 #define IDC_DRAWSCREEN 1002 / Next default values for new objects / #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 131 #define _APS_NEXT_COMMAND_VALUE 32771 #define _APS_NEXT_CONTROL_VALUE 1003 #define _APS_N
16、EXT_SYMED_VALUE 101 #endif #endif Screen.h: / Screen.h : main header file for the SCREEN application / #if !defined(AFX_SCREEN_H_1141ED47_1E96_4C04_B767_6555BE0E67B4_INCLUDED_) #define AFX_SCREEN_H_1141ED47_1E96_4C04_B767_6555BE0E67B4_INCLUDED_ #if _MSC_VER 1000 #pragma once #endif / _MSC_VER 1000 #
17、ifndef _AFXWIN_H_ #error include stdafx.h before including this file for PCH #endif #include resource.h/ main symbols / / CScreenApp: / See Screen.cpp for the implementation of this class / class CScreenApp : public CWinApp public: CScreenApp(); / Overrides / ClassWizard generated virtual function o
18、verrides /AFX_VIRTUAL(CScreenApp) public: virtual BOOL InitInstance(); /AFX_VIRTUAL / Implementation /AFX_MSG(CScreenApp) / NOTE - the ClassWizard will add and remove member functions here. / DO NOT EDIT what you see in these blocks of generated code ! /AFX_MSG DECLARE_MESSAGE_MAP() ; / /AFX_INSERT_
19、LOCATION / Microsoft Visual C+ will insert additional declarations immediately before the previous line. #endif / !defined(AFX_SCREEN_H_1141ED47_1E96_4C04_B767_6555BE0E67B4_INCLUDED_) ScreenDlg.h: / ScreenDlg.h : header file / #include TCPServer.h #if !defined(AFX_SCREENDLG_H_9DB60DB4_6CA7_45EC_997B
20、_1FC964D08D10_INCLUDED_) #define AFX_SCREENDLG_H_9DB60DB4_6CA7_45EC_997B_1FC964D08D10_INCLUDED_ #if _MSC_VER 1000 #pragma once #endif / _MSC_VER 1000 / / CScreenDlg dialog class CScreenDlg : public CDialog / Construction protected: bool run; BITMAP bitmap; int size; char * bmpdata; void GetScreen();
21、 void DrawScreen(); public: CTCPClientSocket clientsocket; CScreenDlg(CWnd* pParent = NULL);/ standard constructor / Dialog Data /AFX_DATA(CScreenDlg) enum IDD = IDD_SCREEN_DIALOG ; CStaticm_drawscreen; /AFX_DATA / ClassWizard generated virtual function overrides /AFX_VIRTUAL(CScreenDlg) protected:
22、virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support /AFX_VIRTUAL / Implementation protected: HICON m_hIcon; / Generated message map functions /AFX_MSG(CScreenDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR O
23、nQueryDragIcon(); afx_msg void OnStart(); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg void OnTimer(UINT nIDEvent); afx_msg void OnStop(); /AFX_MSG DECLARE_MESSAGE_MAP() ; /AFX_INSERT_LOCATION / Microsoft Visual C+ will insert additional declarations immediately before the previous line.
24、 #endif / !defined(AFX_SCREENDLG_H_9DB60DB4_6CA7_45EC_997B_1FC964D08D10_INCLUDED_) StdAfx.h: / stdafx.h : include file for standard system include files, / or project specific include files that are used frequently, but / are changed infrequently / #define _WIN32_WINNT 0 x0500 #if !defined(AFX_STDAF
25、X_H_19AD48ED_1769_499E_925D_EC594502C8AD_INCLUDED_) #define AFX_STDAFX_H_19AD48ED_1769_499E_925D_EC594502C8AD_INCLUDED_ #if _MSC_VER 1000 #pragma once #endif / _MSC_VER 1000 #define VC_EXTRALEAN/ Exclude rarely-used stuff from Windows headers #include / MFC core and standard components #include / MF
26、C extensions #include / MFC Automation classes #include / MFC support for Internet Explorer 4 Common Controls #ifndef _AFX_NO_AFXCMN_SUPPORT #include / MFC support for Windows Common Controls #endif / _AFX_NO_AFXCMN_SUPPORT /AFX_INSERT_LOCATION / Microsoft Visual C+ will insert additional declaratio
27、ns immediately before the previous line. #endif / !defined(AFX_STDAFX_H_19AD48ED_1769_499E_925D_EC594502C8AD_INCLUDED_) TCPServer.h: / TCPServer.h: interface for the CTCPServer class. / / #include #include #if !defined(AFX_TCPSERVER_H_4EBA8D8C_CC6E_4C73_808F_3FA7AE7F983D_INCLUDED_) #define AFX_TCPSE
28、RVER_H_4EBA8D8C_CC6E_4C73_808F_3FA7AE7F983D_INCLUDED_ #if _MSC_VER 1000 #pragma once #endif / _MSC_VER 1000 class CTCPClientEvent public: virtual void OnClose(CSocket * socket, int nErrorCode ); virtual void OnReceive(CSocket * socket, int nErrorCode ); ; class CTCPServerEvent:public CTCPClientEvent
29、 public: virtual void OnAccept(CSocket * socket, int nErrorCode ); ; class CTCPServerSocket; class CTCPClientSocket:public CSocket private: CTCPClientEvent * clientevent; CTCPServerSocket *serversocket; public: CTCPClientSocket(CTCPServerSocket *ServerSocket = NULL); void SetClientEvent(CTCPClientEv
30、ent * ClientEvent); virtual void OnClose(int nErrorCode ); virtual void OnReceive(int nErrorCode ); ; class CTCPServerSocket:public CSocket private: CTCPClientSocket *clientsocket; CTCPServerEvent *serverevent; public: CPtrList ClientList; public: CTCPServerSocket(); void SetServerEvent(CTCPServerEv
31、ent *ServerEvent); virtual void OnAccept(int nErrorCode ); ; #endif / !defined(AFX_TCPSERVER_H_4EBA8D8C_CC6E_4C73_808F_3FA7AE7F983D_INCLUDED_) Screen.cpp: / Screen.cpp : Defines the class behaviors for the application. / #include stdafx.h #include Screen.h #include ScreenDlg.h #ifdef _DEBUG #define
32、new DEBUG_NEW #undef THIS_FILE static char THIS_FILE = _FILE_; #endif / / CScreenApp BEGIN_MESSAGE_MAP(CScreenApp, CWinApp) /AFX_MSG_MAP(CScreenApp) / NOTE - the ClassWizard will add and remove mapping macros here. / DO NOT EDIT what you see in these blocks of generated code! /AFX_MSG ON_COMMAND(ID_
33、HELP, CWinApp:OnHelp) END_MESSAGE_MAP() / / CScreenApp construction CScreenApp:CScreenApp() / TODO: add construction code here, / Place all significant initialization in InitInstance / / The one and only CScreenApp object CScreenApp theApp; / / CScreenApp initialization BOOL CScreenApp:InitInstance(
34、) :AfxSocketInit(); 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 _AFXDLL Enable3dControls();/ Ca
35、ll this when using MFC in a shared DLL #else Enable3dControlsStatic(); / Call this when linking to MFC statically #endif CScreenDlg dlg; m_pMainWnd = int nResponse = dlg.DoModal(); if (nResponse = IDOK) / TODO: Place code here to handle when the dialog is / dismissed with OK else if (nResponse = IDC
36、ANCEL) / 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; ScreenDlg.cpp: / ScreenDlg.cpp : implementation file / #include stdaf
37、x.h #include Screen.h #include ScreenDlg.h #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE = _FILE_; #endif / / CAboutDlg dialog used for App About class CAboutDlg : public CDialog public: CAboutDlg(); / Dialog Data /AFX_DATA(CAboutDlg) enum IDD = IDD_ABOUTBOX ; /AFX_DATA
38、/ ClassWizard generated virtual function overrides /AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support /AFX_VIRTUAL / Implementation protected: /AFX_MSG(CAboutDlg) /AFX_MSG DECLARE_MESSAGE_MAP() ; CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD) /AFX_D
39、ATA_INIT(CAboutDlg) /AFX_DATA_INIT void CAboutDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CAboutDlg) /AFX_DATA_MAP BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) /AFX_MSG_MAP(CAboutDlg) / No message handlers /AFX_MSG_MAP END_MESSAGE_MAP() / / CScreenDlg dialog CScreenDlg
40、:CScreenDlg(CWnd* pParent /*=NULL*/) : CDialog(CScreenDlg:IDD, pParent) /AFX_DATA_INIT(CScreenDlg) / NOTE: the ClassWizard will add member initialization here /AFX_DATA_INIT / Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME); void CS
41、creenDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CScreenDlg) DDX_Control(pDX, IDC_DRAWSCREEN, m_drawscreen); /AFX_DATA_MAP BEGIN_MESSAGE_MAP(CScreenDlg, CDialog) /AFX_MSG_MAP(CScreenDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BU
42、TTON1, OnStart) ON_WM_SIZE() ON_WM_TIMER() ON_BN_CLICKED(IDC_BUTTON2, OnStop) /AFX_MSG_MAP END_MESSAGE_MAP() / / CScreenDlg message handlers BOOL CScreenDlg:OnInitDialog() CDialog:OnInitDialog(); / Add About. menu item to system menu. / IDM_ABOUTBOX must be in the system command range. ASSERT(IDM_AB
43、OUTBOX 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 dialog SetIcon(m_hIcon, TRUE);/ Set big icon SetIcon(m_hIcon, FALSE);/ S
44、et small icon / TODO: Add extra initialization here return TRUE; / return TRUE unless you set the focus to a control void CScreenDlg:OnSysCommand(UINT nID, LPARAM lParam) if (nID dlgAbout.DoModal(); else CDialog:OnSysCommand(nID, lParam); / If you add a minimize button to your dialog, you will need
45、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 CScreenDlg:OnPaint() if (IsIconic() CPaintDC dc(this); / device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); / Cente
46、r icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect( int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; / Draw the icon dc.DrawIcon(x, y, m_hIcon); else CDialog:OnPaint(); / The syste
47、m calls this to obtain the cursor to display while the user drags / the minimized window. HCURSOR CScreenDlg:OnQueryDragIcon() return (HCURSOR) m_hIcon; void CScreenDlg:OnStart() clientsocket.Create(); run = false; bool ret = clientsocket.Connect(127.0.0.1,1033); if (!ret) return; this-SetTimer(0,10
48、00,NULL); void CScreenDlg:OnSize(UINT nType, int cx, int cy) CDialog:OnSize(nType, cx, cy); if (!IsWindow(m_drawscreen.GetSafeHwnd() return; CRect rect; m_drawscreen.GetClientRect( rect.top = 50; rect.left = 10; rect.right = cx -10; rect.bottom = cy - 10; m_drawscreen.MoveWindow( void CScreenDlg:OnT
49、imer(UINT nIDEvent) if (nIDEvent != 0) return; if (run) return; run = true; GetScreen(); run = false; CDialog:OnTimer(nIDEvent); void CScreenDlg:GetScreen() char *buffer = M; clientsocket.Send(buffer,strlen(buffer); clientsocket.Receive( size = bitmap.bmWidthBytes * bitmap.bmHeight; bmpdata = new ch
50、arsize; char * data = bmpdata; int len,receivelen; len = receivelen = 0; buffer = D; clientsocket.Send(buffer,strlen(buffer); do receivelen = clientsocket.Receive(data,size); len += receivelen; data += receivelen; while(lenStretchBlt(0,0,rect.Width(),rect.Height(), void CScreenDlg:OnStop() this-Kill
51、Timer(0); clientsocket.ShutDown(2); clientsocket.Close(); TCPServer.cpp: / TCPServer.cpp: implementation of the CTCPServer class. / / #include stdafx.h #include Server.h #include TCPServer.h #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE=_FILE_; #define new DEBUG_NEW #endif / / Construction/De
52、struction / /CTCPServerEvent/ void CTCPServerEvent:OnAccept(CSocket * socket, int nErrorCode ) void CTCPClientEvent:OnClose(CSocket * socket, int nErrorCode ) void CTCPClientEvent:OnReceive(CSocket * socket, int nErrorCode ) /CTCPClientSocket/ CTCPClientSocket:CTCPClientSocket(CTCPServerSocket *Serv
53、erSocket) :CSocket() serversocket = ServerSocket; void CTCPClientSocket:SetClientEvent(CTCPClientEvent * ClientEvent) this-clientevent = ClientEvent; void CTCPClientSocket:OnClose(int nErrorCode ) CSocket:OnClose(nErrorCode); if (clientevent != NULL) clientevent-OnClose(this,nErrorCode); POSITION po
54、s = serversocket-ClientList.GetHeadPosition(); POSITION oldpos; while (pos != NULL) oldpos = pos; CTCPClientSocket *socket = (CTCPClientSocket*)serversocket-ClientList.GetNext(pos); if (socket != NULL) serversocket-ClientList.RemoveAt(oldpos); void CTCPClientSocket:OnReceive(int nErrorCode ) CSocket:OnReceive(nErrorCode); if (clientevent != NULL) clientevent-OnReceive(this,nErrorC
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 荆州市监利市事业单位2025年统一公开招聘笔试历年典型考题及考点剖析附带答案详解
- 随州市曾都区事业单位2025年统一公开招聘笔试历年典型考题及考点剖析附带答案详解
- 【扬州】2025年江苏扬州高新技术产业开发区下属单位招聘员额制工作人员4人笔试历年典型考题及考点剖析附带答案详解
- 张娟诗经教学课件
- 2025年西安市事业单位公开招聘(募)工作人员笔试和安排笔试历年典型考题及考点剖析附带答案详解
- 【安阳】2025年河南安阳市殷都区区直事业单位公开选调工作人员34人笔试历年典型考题及考点剖析附带答案详解
- 第七节气体钢瓶的常用标记及使用注意事项66课件
- 传统节日教学设计课件
- 小学生篮球拍球活动课件
- 小学生科学课件
- 小数乘除法竖式计算题及答案
- 2024年医院信息保密制度范本(三篇)
- 第22章 相似形 单元检测题2023-2024学年沪科版数学九年级上册
- 血管内超声IVUS简介
- DL∕T 2528-2022 电力储能基本术语
- 山东财经大学《大学英语》2022-2023学年期末试卷
- 2024年歌尔股份有限公司校园招聘考试试题完美版
- peskin量子场论课后答案(芝加哥大学版)
- 医院专家工作站合作协议书
- 2023年河北语文高考试题
- 2023年禁毒工作全年工作总结
评论
0/150
提交评论