局域网屏幕监控下面是程序执行结果:程序中使用从CSocket继承来的CTCPClientSocket和CTCPServerSocket来相互通信。并由客户端使用OnStart()建立连接,通信地址选用回环地址127.0.0.1来进行测试,如果要监控特定主机,须输入该机ip地址:voidCScreenDlg::OnStart(){clientsocket.Create();run=false;boolret=clientsocket.Connect(127.0.0.1,1033);//连接到1033端口if(!ret)return;this-SetTimer(0,1000,NULL);//设置时间间隔是1秒}服务器端进行监听:BOOLCServerDlg::OnInitDialog(){CDialog::OnInitDialog();//AddAbout...menuitemtosystemmenu.//IDM_ABOUTBOXmustbeinthesystemcommandrange.ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX0xF000);CMenu*pSysMenu=GetSystemMenu(FALSE);if(pSysMenu!=NULL){CStringstrAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if(!strAboutMenu.IsEmpty()){pSysMenu-AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);}}//Settheiconforthisdialog.Theframeworkdoesthisautomatically//whentheapplication'smainwindowisnotadialogSetIcon(m_hIcon,TRUE);//SetbigiconSetIcon(m_hIcon,FALSE);//Setsmallicon//TODO:Addextrainitializationhereboolret=false;serversocket.SetServerEvent(this);ret=serversocket.Create(1033);//打开1033端口ret=serversocket.Listen();//进行监听returnTRUE;//returnTRUEunlessyousetthefocustoacontrol}建立连接后,服务器端使用GetScreen获取数据:并转换成2进制信息:voidCServerDlg::GetScreen(){CDCdc,bmpdc;intwidth,height;dc.CreateDC(DISPLAY,NULL,NULL,NULL);CBitmapbm;width=GetSystemMetrics(SM_CXSCREEN);height=GetSystemMetrics(SM_CYSCREEN);bm.CreateCompatibleBitmap(&dc,width,height);bmpdc.CreateCompatibleDC(&dc);bmpdc.SelectObject(&bm);bmpdc.BitBlt(0,0,width,height,&dc,0,0,SRCCOPY);bm.GetBitmap(&bitmap);size=bitmap.bmWidthBytes*bitmap.bmHeight;bmpdata=newchar[size];BITMAPINFOHEADERbih;bih.biBitCount=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,bmpdata,(BITMAPINFO*)&bih,DIB_RGB_COLORS);}发送图片数据:voidCServerDlg::SendBitData(CSocket*socket){char*data=bmpdata;intsendlen=0;intlen=0;do//发送图像数据{sendlen=socket-Send(data,size);len+=sendlen;data+=sendlen;}while(lensize);deletebmpdata;bmpdata=NULL;size=0;}发送图片结构:voidCServerDlg::SendBitmap(CSocket*socket){GetScreen();socket-Send(&bitmap,sizeof(BITMAP));}客户端使用GetScreen()来接送二进制数据:voidCScreenDlg::GetScreen(){char*buffer=M;//获取图像结构信息clientsocket.Send(buffer,strlen(buffer));clientsocket.Receive(&bitmap,sizeof(BITMAP));size=bitmap.bmWidthBytes*bitmap.bmHeight;bmpdata=newchar[size];//处理图像的动态数组char*data=bmpdata;intlen,receivelen;len=receivelen=0;buffer=D;//获取图像数据clientsocket.Send(buffer,strlen(buffer));do{receivelen=clientsocket.Receive(data,size);len+=receivelen;data+=receivelen;}while(lensize);DrawScreen();deletebmpdata;bmpdata=NULL;size=0;}使DrawScreen()来在窗口中绘制接收到的图形:voidCScreenDlg::DrawScreen(){CDC*dc=m_drawscreen.GetDC();BITMAPINFOHEADERbih;bih.biBitCount=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;CBitmapbm;bm.CreateBitmapIndirect(&bitmap);CDCbmpdc;bmpdc.CreateCompatibleDC(dc);SetDIBits(bmpdc.m_hDC,bm,0,bitmap.bmHeight,bmpdata,(BITMAPINFO*)&bih,DIB_RGB_COLORS);bmpdc.SelectObject(&bm);CRectrect;m_drawscreen.GetClientRect(&rect);dc-StretchBlt(0,0,rect.Width(),rect.Height(),&bmpdc,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);}当关闭窗口时会调用OnStopf来做处理:voidCScreenDlg::OnStop(){this-KillTimer(0);//结束计时clientsocket.ShutDown(2);clientsocket.Close();//关闭套接字}另外,继承自CSocket的CTCPClentSocket和CTCPServerSocket分别定义了用于接收的OnAccept()用于发送的OnRecieve()用于关闭套接字的OnClose();OnAccept:voidCTCPServerSocket::OnAccept(intnErrorCode){CSocket::OnAccept(nErrorCode);clientsocket=newCTCPClientSocket(this);this-Accept(*clientsocket);POSITIONpos=ClientList.GetHeadPosition();ClientList.InsertBefore(pos,clientsocket);if(serverevent!=NULL)clientsocket-SetClientEvent(serverevent);if(serverevent!=NULL)serverevent-OnAccept(this,nErrorCode);}OnRecieve:voidCTCPClientSocket::OnReceive(intnErrorCode){CSocket::OnReceive(nErrorCode);if(clientevent!=NULL)clientevent-OnReceive(this,nErrorCode);}OnClose:voidCTCPClientSocket::OnClose(intnErrorCode){CSocket::OnClose(nErrorCode);if(clientevent!=NULL)clientevent-OnClose(this,nErrorCode);POSITIONpos=serversocket-ClientList.GetHeadPosition();POSITIONoldpos;while(pos!=NULL){oldpos=pos;CTCPClientSocket*socket=(CTCPClientSocket*)serversocket-ClientList.GetNext(pos);if(socket!=NULL)serversocket-ClientList.RemoveAt(oldpos);}}课程设计总结:经过的了一周的学习和实践,加深了对网络程序设计的理解和应用。理解和掌握了网络套接字的用法,使用规则。在编程上拓宽了自己的思路,学会了一些灵活的方法,熟练了编程技巧。这次的课程设计中不仅检验了我所学习的知识,也培养了我如何去把握一件事情,如何去做一件事情,又如何完成一件事情。在设计过程中,与同学分工设计,和同学们相互探讨,相互学习,相互监督。对于如何参考文献有了更深的理解。参考资料:WINDOWS网络与通信设计王艳平VISUALC++范例完全自学手册附源代码:Screen工程中:Resourse.h//{{NO_DEPENDENCIES}}//MicrosoftDeveloperStudiogeneratedincludefile.//UsedbyScreen.rc//#defineIDM_ABOUTBOXcreen0x0010#defineIDD_ABOUTBOX100#defineIDS_ABOUTBOX101#defineIDD_SCREE