基于C局域網視頻聊天設計說明_第1頁
基于C局域網視頻聊天設計說明_第2頁
基于C局域網視頻聊天設計說明_第3頁
基于C局域網視頻聊天設計說明_第4頁
基于C局域網視頻聊天設計說明_第5頁
已閱讀5頁,還剩30頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

....34/351緒論視頻監(jiān)控是各行業(yè)重點部門或重要場所進行實時監(jiān)控的物理基礎,管理部門可通過它獲得有效數(shù)據、圖像或聲音信息,對突發(fā)性異常事件的過程進行與時的監(jiān)視和記憶,用以提供高效、與時地指揮和高度、布置警力、處理案件等。本系統(tǒng)采用DirectShow網絡組播技術實現(xiàn)了視頻捕獲、視頻壓縮、網絡傳輸、視頻解碼和實時回放,減小了網絡帶寬占用,高效的傳輸視頻數(shù)據,獨立于硬件。可擴展性好。完全利用現(xiàn)有1P數(shù)據網絡傳輸數(shù)據.不需要單獨布線,顯著降低了系統(tǒng)成本,縮短了系統(tǒng)開發(fā)周期,并且可以容易的實現(xiàn)遠程監(jiān)測2Windows服務一個Windows服務程序是在Windows操作系統(tǒng)下能完成特定功能的可執(zhí)行的應用程序。Windows服務程序雖然是可執(zhí)行的,但是它不像一般的可執(zhí)行文件通過雙擊就能開始運行了,它必須有特定的啟動方式。這些啟動方式包括了自動啟動和手動啟動兩種。對于自動啟動的Windows服務程序,它們在Windows啟動或是重啟之后用戶登錄之前就開始執(zhí)行了。而對于手動啟動的Windows服務程序,你可以通過命令行工具的NETSTART命令來啟動它,或是通過控制面板中管理工具下的服務一項來啟動相應的Windows服務程序。同樣,一個Windows服務程序也不能像一般的應用程序那樣被終止。因為Windows服務程序一般是沒有用戶界面的,所以你也要通過命令行工具或是下面圖中的工具來停止它,或是在系統(tǒng)關閉時使得Windows服務程序自動停止。因為Windows服務程序沒有用戶界面,為了能使一個Windows服務程序能夠正常并有效的在系統(tǒng)環(huán)境下工作,程序員必須實現(xiàn)一系列的方法來完成其服務功能。Windows服務程序的應用圍很廣,典型的Windows服務程序包含了硬件控制、應用程序監(jiān)視、系統(tǒng)級應用、診斷、報告、Web和文件系統(tǒng)服務等功能。2.1添加文件監(jiān)視服務將生成的服務名為Webcamservice的服務添加視頻監(jiān)視功能:首先,在C盤創(chuàng)建文件夾w將程序生成的debug中的文件復制到w文件夾在C/windows/搜索installutil.exe執(zhí)行文件,將其復制到w文件夾啟動cmd,打開命令提示符窗體鍵入如圖2-1所示圖2-1添加服務功能用netstart命令啟動服務Webcamservice如圖2-2所示圖2-2啟動Webcamservice3項目的設計原理3.1DirectShow技術采用網絡攝像機的遠程視頻監(jiān)控具有錄像時間長、圖像質量好、查詢速度快等優(yōu)點,目前應用非常廣泛。對于網絡攝像機傳輸?shù)囊曨l數(shù)據,需要專門的Filter來處理并在DirectShow的框架下或回放,或保存。監(jiān)控服務器通過Internet/Intranet輪詢網絡攝像機獲取視頻。本文以視頻數(shù)據接收Filter的設計過程介紹基于DirectShow的視頻數(shù)據流的傳輸以與通過自定義的通訊協(xié)議的數(shù)據解析過程和Filter程序設計與實現(xiàn)過程。DirectShow[1]是微軟公司在ActiveMovie和VideoforWindows的基礎上推出的基于COM的流媒體處理的開發(fā)包,與DirectX開發(fā)包一起發(fā)布。DirectShow為多媒體流的捕捉和回放以與二次開發(fā)提供了強有力的支持。運用DirectShow,可以很方便地從支持WDM驅動模型的采集卡上采集數(shù)據,并且調用其API函數(shù)進行后期處理。它廣泛地支持各種媒體格式,包括Asf,Mpeg,Avi,Dv,Mp3,Wave等等,使得多媒體數(shù)據的回放變得輕而易舉。DirectShow是一個開放的框架,因此只要有合適的Filter來分析和解碼,可以支持任何格式。3.2TCP/IP協(xié)議在TCP/IP協(xié)議組分兩種協(xié)議:網絡層的協(xié)議,應用層的協(xié)議網絡層協(xié)議網絡層協(xié)議管理離散的計算機間的數(shù)據傳輸。這些協(xié)議是在系統(tǒng)表層以下工作的。比如,IP協(xié)議為用戶和遠程計算機提供了信息包的傳輸方法。它是在許多信息的基礎上工作的,好比說是機器的IP地址。在機器IP地址和其它信息的基礎上,IP確保信息包能正確地到達目的機器。通過這一過程,IP和其它網絡層的協(xié)議共同用于數(shù)據傳輸。如果沒有網絡工具,用戶就看不到在系統(tǒng)里工作的IP。應用層協(xié)議相反地,應用層協(xié)議用戶是可以看得到的。比如,文件傳輸協(xié)議(FTP)用戶是看得到的。用戶為了傳輸一個文件請求一個和其它計算機的連接,連接建立后,就開始傳輸文件。在傳輸時,用戶和遠程計算機的交換的一部分是能看到的。IPIP層接收由更低層(網絡接口層例如以太網設備驅動程序)發(fā)來的數(shù)據包,并把該數(shù)據包發(fā)送到更高層TCP或UDP層;相反,IP層也把從TCP或UDP層接收來的數(shù)據包傳送到更低層。IP數(shù)據包是不可靠的,因為IP并沒有做任何事情來確認數(shù)據包是按順序發(fā)送的或者沒有被破壞。IP數(shù)據包中含有發(fā)送它的主機的地址(源地址)和接收它的主機的地址(目的地址)。高層的TCP和UDP服務在接收數(shù)據包時,通常假設包中的源地址是有效的。也可以這樣說,IP地址形成了許多服務的認證基礎,這些服務相信數(shù)據包是從一個有效的主機發(fā)送來的。TCP如果IP數(shù)據包中有已經封好的TCP數(shù)據包,那么IP將把它們向上傳送到TCP層。TCP將包排序并進行錯誤檢查,同時實現(xiàn)虛電路間的連接。TCP數(shù)據包中包括序號和確認,所以未按照順序收到的包可以被排序,而損壞的包可以被重傳。TCP將它的信息送到更高層的應用程序,例如Telnet的服務程序和客戶程序。應用程序輪流將信息送回TCP層,TCP層便將它們向下傳送到IP層,設備驅動程序和物理介質,最后到接收方。3.3C/S架構服務器客戶端客戶端...在網絡連接模式中除對等網外,還有另一種形式的網絡,即客戶機/服務器網,Client/Server。在客戶機/服務器網絡中,服務器是網絡的核心,而客戶機是網絡的基礎,客戶機依靠服務器獲得所需要的網絡資源,而服務器為客戶機提供網絡必須的資源。服務器客戶端客戶端...圖3-1c/s結構4程序流程圖與設計4.1程序時序圖與系統(tǒng)架構本系統(tǒng)采用面向連接的客戶/服務模型,服務器必須首先啟動,否則客戶進程的Connect()系統(tǒng)調用將返回錯誤代碼表示連接失敗。無連接的服務進程也必須首先啟動以指定本地的套接字地址否則客戶進程的數(shù)據服務請求傳送不到服務器進程。面向連接的c/s時序圖如圖4-1所示AAccept()Recv()Send()Close()Listen()Bind()Socket()服務器發(fā)送數(shù)據<<確認發(fā)送數(shù)據<請求建立連接Recv()Close()Send()Connect()Socket()客戶圖4-1程序時序圖系統(tǒng)由服務器終端采集傳輸系統(tǒng)和客戶端接收系統(tǒng)兩部分組,系統(tǒng)構架如圖4-2所示攝像頭攝像頭采集卡服務器服務程序網絡傳輸模塊網絡接收模塊客戶端程序解碼顯示視頻流控制指令視頻流控制指令圖4-2系統(tǒng)架構4.2程序設計分析4.2.1任務目標服務器端程序目標:服務器服務器端服務程序進行數(shù)據采集(捕捉攝像頭捕獲數(shù)據),提供IP端口實現(xiàn)數(shù)據流的傳輸??蛻舳顺绦蚰繕耍嚎蛻舳顺绦蛲ㄟ^IP協(xié)議與服務器端通信,接收并回放服務器端采集的視頻數(shù)據流。4.2.2程序描述Socket類:socket之間的連接可以分為三種類型:客戶端連接,監(jiān)聽連接以與服務器端連接。客戶端連接是指由客戶端的socket提出連接請求,要連接的目標是服務器端的socket。為此,客戶端的socket必須首先描述它要連接的服務器端socket(主要是指服務器端socket的地址和端口號),然后再定位所要連接的服務器端socket,找到以后,就向服務器端socket請求連接。當然,服務器端的socket此時未必正好處于準備好狀態(tài),不過,服務器端的socket會自動維護客戶請求連接的隊列,然后在它認為合適的時候向客戶端socket發(fā)出"允許連接"(accept)的信號,這時客戶端socket與服務器端socket的連接就建立了。監(jiān)聽連接,服務器端socket并不定位具體的客戶端socket,而是處于等待連接的狀態(tài)。當服務器端socket監(jiān)聽到或者說接收到客戶端socket的連接請求,它就響應客戶端socket的請求建立一個新的socket句柄并與客戶端連接,而服務器端socket繼續(xù)處于監(jiān)聽狀態(tài),還可以接收其它客戶端socket的連接請求。服務器端連接,是指當服務器端socket接收到客戶端socket的連接請求后,就把服務器端socket的描述發(fā)給客戶端,一旦客戶端確認了此描述,連接就建立了。在本文中的聊天程序用的就是監(jiān)聽連接,即服務器設置連接個數(shù)后進行監(jiān)聽,客戶端進行對服務器端的連接,這樣就可以進行相互通信了。TcpService類namespaceTCP{internalclassTcpServer:IDisposable{//Thisisnotthemaxnumberofconnectionsyoucanhave,it'sthenumber//thatcanqueueupwaitingforyoutoAcceptthem.IfmorethanMAXCONNECTION//moreclientstrytoconnectwhileyouareservicinganother,OnConnectis//probablytakingtoolong.constintMAXCONNECTIONS=3;#regionMembervariablesprivateArrayListm_aryClients;privateSocketm_sockListener;privatevolatileboolm_bShuttingDown;privateManualResetEventShutDownReady;#endregion//ReturnanarrayoftheipaddressesassignedtothispcpublicstaticIPAddress[]GetAddresses(){IPAddress[]aryLocalAddr=null;stringstrHostName="";//NOTE:DNSlookupsareniceandallbutquitetimeconsuming.strHostName=Dns.GetHostName();#ifUSING_NET11IPHostEntryipEntry=Dns.GetHostByName(strHostName);#elseIPHostEntryipEntry=Dns.GetHostEntry(strHostName);#endifaryLocalAddr=ipEntry.AddressList;//VerifywegotanIPaddress.if(aryLocalAddr==null||aryLocalAddr.Length<1){thrownewException("Unabletogetlocaladdress");}returnaryLocalAddr;}publicTcpServer(intnPortListen){_TcpServer(nPortListen,GetAddresses()[0]);}publicTcpServer(intnPortListen,IPAddressip){_TcpServer(nPortListen,ip);}//ShutdownthelistenerpublicvoidDispose(){//ShuttingdownisarealPITA.Youcan'tclosethelistener//whilethereisanoutstandingasynccallactive.Andyoucan't//canceltheasynccall.Grr.Asaworkaround,thisroutine//makesaconnectiontotheport.TheOnConnectroutine,recognizing//thatweareinshutdown,doesn'tcreateanewasynccall.TcpClientt=null;//Onlywantonethreadtobepreformingshutdownatatime.lock(this){//Havewealreadyshutdown?if(!m_bShuttingDown){m_bShuttingDown=true;//Disconnecteachclientforeach(SockWrappersinm_aryClients){try{s.Client.Shutdown(SocketShutdown.Both);s.Client.Close();}catch{}}m_aryClients=null;//ConnecttotheporttotriggertheasynclistenerIPEndPointep=(IPEndPoint)m_sockListener.LocalEndPoint;t=newTcpClient(ep.Address.ToString(),ep.Port);}}if(t!=null){//Listenfortheasynclistenertoletgo.Thismustbedone//outsidethecritsectionsincethelistenerneedstolockit.ShutDownReady.WaitOne(3000,false);lock(this){//closeeverythingdownm_sockListener.Close();m_sockListener=null;t.Close();ShutDownReady.Close();ShutDownReady=null;}}}~TcpServer(){//IfDisposeisnotcalledagainstourclassandthedestructoris//called,someofthemembervariablesinthisclasshavealready//beendisposed.Suchbeingthecase,there'snowaytocleanup//nicely.Moral:AlwayscallDispose.//Dispose();}//SendtoallconnectedclientspublicvoidSendToAll(MemoryStreamm){_SendToAll(m.GetBuffer(),(int)m.Length);}publicvoidSendToAll(byte[]b){_SendToAll(b,b.Length);}publicintConnections{get{returnm_aryClients.Count;}}publiceventTcpConnectedConnected;publiceventTcpConnectedDisconnected;publiceventTcpReceiveDataReceived;publiceventTcpSendSend;privatevoid_TcpServer(intnPortListen,IPAddressip){try{//Initializemembervarsm_aryClients=newArrayList(5);ShutDownReady=newManualResetEvent(false);m_bShuttingDown=false;//Createthelistenersocketinthismachine'sIPaddressm_sockListener=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);m_sockListener.Bind(newIPEndPoint(ip,nPortListen));m_sockListener.Listen(MAXCONNECTIONS);//Setupacallbacktobenotifiedofconnectionrequestsm_sockListener.BeginAccept(newAsyncCallback(OnConnectRequest),m_sockListener);}catch{m_bShuttingDown=true;throw;}}privatevoid_SendToAll(byte[]b,intiLength){lock(this){if(!m_bShuttingDown){foreach(SockWrappersinm_aryClients){_SendOne(s,b,iLength);}}}}privatevoid_SendOne(SockWrappers,byte[]b,intiLength){try{boolbSend=true;if(Send!=null)Send(this,refs.obj,refbSend);if(bSend)s.Client.Send(b,iLength,SocketFlags.None);}catch{//Ignoretheerror.Iftheclientisdead,OnReceiveData//willbecalledtoclosetheconnection.Iwouldremoveit//anyway,exceptbadthingshappenifyouremoveanentry//fromalistwhileusingforeach.}}//ClienthasconnectedprivatevoidOnConnectRequest(IAsyncResultar){//GetthelistenerandclientSocketlistener=(Socket)ar.AsyncState;Socketclient=listener.EndAccept(ar);lock(this){if(!m_bShuttingDown){//WraptheclientandaddittothearraySockWrappers=newSockWrapper(client);m_aryClients.Add(s);//FiretheConnectedeventif(Connected!=null)Connected(this,refs.obj);//SetupanasyncwaitforpacketsfromtheclientAsyncCallbackreceiveData=newAsyncCallback(OnReceivedData);s.Client.BeginReceive(s.byBuff,0,s.byBuff.Length,SocketFlags.None,receiveData,s);//(Re)Setupacallbacktobenotifiedofconnectionrequestslistener.BeginAccept(newAsyncCallback(OnConnectRequest),listener);}else{//Ifweareinshutdownmode,DON'Tadd//theconnectiontothearray,DON'Tsetup//theasynclisten,andDOsettheevent//tosaywearedone.ShutDownReady.Set();}}} //Clienthassentdata,orhasdisconnectedprivatevoidOnReceivedData(IAsyncResultar){//SocketwasthepassedinobjectSockWrappers=(SockWrapper)ar.AsyncState;lock(this){if(!m_bShuttingDown){//Checkifwegotanydatatry{intnBytesRec=s.Client.EndReceive(ar);if(nBytesRec>0){if(DataReceived!=null)DataReceived(this,refs.obj,refs.byBuff,nBytesRec);//RestablishthecallbackAsyncCallbackreceiveData=newAsyncCallback(OnReceivedData);s.Client.BeginReceive(s.byBuff,0,s.byBuff.Length,SocketFlags.None,receiveData,s);}else{//IfnodatawasreceivedthentheconnectionisprobablydeadRemoveConnection(s);}}catch{RemoveConnection(s);}}}}//RemoveaconnectionfromthelistofactiveconnectionsprivatevoidRemoveConnection(SockWrappers){try{s.Client.Shutdown(SocketShutdown.Both);s.Client.Close();}catch{}//Removeitfromthearraytry{m_aryClients.Remove(s);}catch{}//FiretheDisconnectedeventif(Disconnected!=null)Disconnected(this,refs.obj);}//Wrapperforeachclient(storedinm_aryClients)internalclassSockWrapper{//ThebufferisusedbyreceivepublicSocketClient;publicbyte[]byBuff;publicobjectobj;publicSockWrapper(Socketclient){Client=client;byBuff=newbyte[256];obj=newobject();}}}publicdelegatevoidTcpConnected(Objectsender,refobjecto);publicdelegatevoidTcpSend(Objectsender,refobjecto,refboolb);publicdelegatevoidTcpReceive(Objectsender,refobjecto,refbyte[]b,intByteCount);}WebCamService類:namespaceWebCamService{publicclassWebCamService:ServiceBase{ #regionRequiredServiceRelatedMethodsprivateSystemponentModel.Containercomponents=null;publicWebCamService(){InitializeComponent();}privatevoidInitializeComponent(){components=newSystemponentModel.Container();this.ServiceName="WebCamService";}protectedoverridevoidDispose(booldisposing){if(disposing){if(components!=null){components.Dispose();}}base.Dispose(disposing);} #endregion[STAThread]publicstaticvoidMain(string[]args){Thread.CurrentThread.Name="Mainthread";WebCamServiceServiceToRun=newWebCamService();if(Debugger.IsAttached){ServiceToRun.Run();}else{ServiceToRun.CanPauseAndContinue=false;ServiceBase.Run(ServiceToRun);}} #regionMemberVariablesprivateconstintMAXOUTSTANDINGPACKETS=3;///<summary>///Thethreadwillrunthejob.///ThejobistheMethodRun()below///</summary>protectedThreadthread=null;privateManualResetEventConnectionReady;privatevolatileboolbShutDown;privatevolatileintiConnectionCount;#endregion///<summary>///Setthingsinmotionsoyourservicecandoitswork.///為了服務器可以工作而設置的選項///</summary>protectedoverridevoidOnStart(string[]args){ThreadStartstarter=newThreadStart(Run);//實例化進程thread=newThread(starter);thread.Start();}///<summary>///Stopthisservice.///停止服務///TheRun()Methodtestsforthisthreadstateeachsecond///每秒都為這個進程啟動方法測試///</summary>protectedoverridevoidOnStop(){//Setexitcondition//設置退出狀態(tài)bShutDown=true;//Needtogetoutofwait//需要退出等待ConnectionReady.Set();}publicvoidRun(){constintVIDEODEVICE=0;//zerobasedindexofvideocapturedevicetouseconstintFRAMERATE=15;//Dependsonvideodevicecaps.Generally4-30.constintVIDEOWIDTH=640;//DependsonvideodevicecapsconstintVIDEOHEIGHT=480;//DependsonvideodevicecapsconstlongJPEGQUALITY=30;//1-100or0fordefaultconstintTCPLISTENPORT=399;Capturecam=null;TcpServerserv=null;ImageCodecInfomyImageCodecInfo;EncoderParametersmyEncoderParameters;//Setuplogging//建立日志記錄StreamWritersw=File.AppendText("d:\WebCam.log");try{//SetupmembervarsConnectionReady=newManualResetEvent(false);bShutDown=false;//Setuptcpserver//建立TCP服務iConnectionCount=0;serv=newTcpServer(TCPLISTENPORT,TcpServer.GetAddresses()[3]);serv.Connected+=newTcpConnected(Connected);serv.Disconnected+=newTcpConnected(Disconnected);serv.DataReceived+=newTcpReceive(Receive);serv.Send+=newTcpSend(Send);myEncoderParameters=null;myImageCodecInfo=GetEncoderInfo("image/jpeg");if(JPEGQUALITY!=0){//Ifnotusingthedefaultjpegqualitysetting//如果不適用默認的JPEG質量設置EncoderParametermyEncoderParameter;myEncoderParameter=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,JPEGQUALITY);myEncoderParameters=newEncoderParameters(1);myEncoderParameters.Param[0]=myEncoderParameter;}cam=newCapture(VIDEODEVICE,FRAMERATE,VIDEOWIDTH,VIDEOHEIGHT);//Initializationsucceeded.Now,startservingupframes//初始化成功,現(xiàn)在,啟動服務器發(fā)送幀DoIt(cam,serv,sw,myImageCodecInfo,myEncoderParameters);}catch(Exceptionex){try{sw.WriteLine(String.Format("{0}:Failedonstartup{1}",DateTime.Now.ToString(),ex));}catch{}}finally{//Cleanup//清除if(serv!=null){serv.Dispose();}if(cam!=null){cam.Dispose();}sw.Close();}}//Startservingupframes//啟動服務器松幀privatevoidDoIt(Capturecam,TcpServerserv,StreamWritersw,ImageCodecInfomyImageCodecInfo,EncoderParametersmyEncoderParameters){MemoryStreamm=newMemoryStream(20000);Bitmapimage=null;IntPtrip=IntPtr.Zero;do{//Waittilaclientconnectsbeforewestartthegraph//發(fā)送圖像之前等待客戶端連接ConnectionReady.WaitOne();cam.Start();//Whilenotshuttingdown,andstillatleastoneclient//當不關閉并且至少有一個客戶端連接while((!bShutDown)&&(serv.Connections>0)){try{//captureimage//圖像俘獲ip=cam.GetBitMap();image=newBitmap(cam.Width,cam.Height,cam.Stride,PixelFormat.Format24bppRgb,ip);image.RotateFlip(RotateFlipType.RotateNoneFlipY);//saveittojpegusingqualityoptions//保存為JPEG(默認的格式選項)m.Position=10;image.Save(m,myImageCodecInfo,myEncoderParameters);//Sendthelengthasafixedlengthstring//作為固定的字符串長度發(fā)送長度m.Position=0;m.Write(Encoding.ASCII.GetBytes((m.Length-10).ToString("d8")+"\r\n"),0,10);//sendthejpegimage//發(fā)送格式為GPEG格式的圖像serv.SendToAll(m);//Emptythestream//清空流m.SetLength(0);//removetheimagefrommemory//從存移出所發(fā)送的圖像image.Dispose();image=null;}catch(Exceptionex){try{sw.WriteLine(DateTime.Now.ToString());sw.WriteLine(ex);}catch{}}finally{if(ip!=IntPtr.Zero){Marshal.FreeCoTaskMem(ip);ip=IntPtr.Zero;}}}//Clientshavealldisconnected.Pause,thensleepandwaitformore//客戶端不連接,中斷,等待更多的發(fā)送cam.Pause();sw.WriteLine("Droppedframes:"+cam.m_Dropped.ToString());}while(!bShutDown);}classPacketCount:IDisposable{privateintm_PacketCount;privateintm_MaxPackets;publicPacketCount(inti){m_MaxPackets=i;m_PacketCount=0;}publicboolAddPacket(){boolb;lock(this){b=m_PacketCount<m_MaxPackets;if(b){m_PacketCount++;}else{Debug.WriteLine("MaxoutstandingPacketsreached");}}returnb;}publicvoidRemovePacket(){lock(this){if(m_PacketCount>0){m_PacketCount--;}else{Debug.WriteLine("Packetcountismessedup");}}}publicintCount(){returnm_PacketCount;}#regionIDisposableMemberspublicvoidDispose(){#ifDEBUGif(m_PacketCount!=0){Debug.WriteLine("Packetsleftover:"+m_PacketCount.ToString());}#endif}#endregion}//Aclientattachedtothetcpport//客戶端與TCP端口建立連接privatevoidConnected(objectsender,refobjectt){lock(this){t=newPacketCount(MAXOUTSTANDINGPACKETS);iConnectionCount++;if(iConnectionCount==1){ConnectionReady.Set();}}}//Aclientdetachedfromthetcpport//客戶端與TCP端口撤銷連接privatevoidDisconnected(objectsender,refobjectt){lock(this){iConnectionCount--;if(iConnectionCount==0){ConnectionReady.Reset();}}}privatevoidReceive(Objectsender,refobjecto,refbyte[]b,intByteCount){PacketCountpc=(PacketCount)o;pc.RemovePacket();}privatevoidSend(Objectsender,refobjecto,refboolb){PacketCountpc=(PacketCount)o;b=pc.AddPacket();}//Findtheappropriateencoder//查找合適的編碼器privateImageCodecInfoGetEncoderInfo(StringmimeType){intj;ImageCodecInfo[]encoders=ImageCodecInfo.GetImageEncoders();for(j=0;j<encoders.Length;++j){if(encoders[j].MimeType==mimeType)returnencoders[j];}returnnull;}}}Webclient類:namespaceWebCamClient{///<summary>///SummarydescriptionforForm1.///</summary>publicclassForm1:System.Windows.Forms.Form{DoImagesdoImages;staticintm_Count;privateSystem.Windows.Forms.PictureBoxpictureBox1;privateSystem.Windows.Forms.TextBoxtxtServer;privateSystem.Windows.Forms.ButtonbtnPress;privateSystem.Windows.Forms.Labellabel1;privateSystem.Windows.Forms.TextBoxtxtMessage;privateSystem.Windows.Forms.TextBoxtxtPort;privateSystem.Windows.Forms.Labellabel2;privateSystem.Windows.Forms.Labellabel3;privateSystem.Windows.Forms.Timertimer1;privateSystem.Windows.Forms.TextBoxtxtFrames;privateSystemponentModel.IContainercomponents;publicForm1(){////RequiredforWindowsFormDesignersupport//InitializeComponent();}///<summary>///Cleanupanyresourcesbeingused.///</summary>protectedoverridevoidDispose(booldisposing){if(disposing){if(components!=null){components.Dispose();}}if(doImages!=null){doImages.Done=true;doImages=null;}base.Dispose(disposing);} #regionWindowsFormDesignergeneratedcode///<summary>///RequiredmethodforDesignersupport-donotmodify///thecontentsofthismethodwiththecodeeditor.///</summary>privatevoidInitializeComponent(){thisponents=newSystemponentModel.Container();this.pictureBox1=newSystem.Windows.Forms.PictureBox();this.btnPress=newSystem.Windows.Forms.Button();this.txtServer=newSystem.Windows.Forms.TextBox();this.label1=newSystem.Windows.Forms.Label();this.txtMessage=newSystem.Windows.Forms.TextBox();this.txtPort=newSystem.Windows.Forms.TextBox();this.label2=newSystem.Windows.Forms.Label();this.txtFrames=newSystem.Windows.Forms.TextBox();this.label3=newSystem.Windows.Forms.Label();this.timer1=newSystem.Windows.Forms.Timer(thisponents);((SystemponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();this.SuspendLayout();////pictureBox1//this.pictureBox1.Anchor=((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top|System.Windows.Forms.AnchorStyles.Bottom)|System.Windows.Forms.AnchorStyles.Left)|System.Windows.Forms.AnchorStyles.Right)));this.pictureBox1.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;this.pictureBox1.Location=newSystem.Drawing.Point(0,0);this.pictureBox1.Name="pictureBox1";this.pictureBox1.Size=newSystem.Drawing.Size(508,227);this.pictureBox1.TabIndex=0;this.pictureBox1.TabStop=false;////btnPress//this.btnPress.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));this.btnPress.DialogResult=System.Windows.Forms.DialogResult.Cancel;this.btnPress.Location=newSystem.Drawing.Point(278,322);this.btnPress.Name="btnPress";this.btnPress.Size=newSystem.Drawing.Size(96,35);this.btnPress.TabIndex=1;this.btnPress.Tag="1";this.btnPress.Text="Start";this.btnPress.Click+=newSystem.EventHandler(this.btnPress_Click);////txtServer//this.txtServer.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));this.txtServer.Location=newSystem.Drawing.Point(19,279);this.txtServer.Name="txtServer";this.txtServer.Size=newSystem.Drawing.Size(115,21);this.txtServer.TabIndex=3;this.txtServer.Text="";////label1//this.label1.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));this.label1.Font=newSystem.Drawing.Font("MicrosoftSansSerif",10F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(0)));this.label1.Location=newSystem.Drawing.Point(19,236);this.label1.Name="label1";this.label1.Size=newSystem.Drawing.Size(144,43);this.label1.TabIndex=4;this.label1.Text="Enterservernameoripaddress";////txtMessage//this.txtMessage.Anchor=((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)|System.Windows.Forms.AnchorStyles.Right)));this.txtMessage.Location=newSystem.Drawing.Point(10,374);this.txtMessage.Name="txtMessage";this.txtMessage.ReadOnly=true;this.txtMessage.Size=newSystem.Drawing.Size(488,21);this.txtMessage.TabIndex=5;////txtPort//this.txtPort.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));this.txtPort.Location=newSystem.Drawing.Point(192,279);this.txtPort.Name="txtPort";this.txtPort.Size=newSystem.Drawing.Size(58,21);this.txtPort.TabIndex=6;this.txtPort.Text="399";////label2//this.label2.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));this.label2.Location=newSystem.Drawing.Point(192,253);this.label2.Name="label2";this.label2.Size=newSystem.Drawing.Size(86,25);this.label2.TabIndex=7;this.label2.Text="PortNumber";////txtFrames//this.txtFrames.Anchor=((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)));this.txtFrames.Location=newSystem.Drawing.Point(298,279);this.txtFrames.Name="txtFrames";this.txtFrames.ReadOnly=true;this.txtFrames.Size=newSystem.Drawing.Size(76,2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論