中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

VB.NETSocket編程的示例分析

小編給大家分享一下VB.NET Socket編程的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供大興網(wǎng)站建設(shè)、大興做網(wǎng)站、大興網(wǎng)站設(shè)計(jì)、大興網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、大興企業(yè)網(wǎng)站模板建站服務(wù),10余年大興做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

下面通過例子來學(xué)習(xí)VB.NET Socket編程類的應(yīng)用,下面的程序分別分服務(wù)器和客戶端兩部分:

  1. ImportsSystem  

  2. ImportsSystem.Net  

  3. ImportsSystem.Net.Sockets  

  4. ImportsSystem.Text  

  5. ImportsSystem.Threading  

  6. ImportsMicrosoft.VisualBasic  

  7.  

  8. 'Stateobjectforreadingclientdataasynchronously  

  9.  

  10. PublicClassStateObject  

  11. 'Clientsocket.  

  12. PublicworkSocketAsSocket=Nothing 

  13. 'Sizeofreceivebuffer.  

  14. PublicConstBufferSizeAsInteger=1024 

  15. 'Receivebuffer.  

  16. Publicbuffer(BufferSize)AsByte  

  17. 'Receiveddatastring.  

  18. PublicsbAsNewStringBuilder  

  19. EndClass'StateObject  

  20.  

  21.  

  22. PublicClassAsynchronousSocketListener  

  23. 'Threadsignal.  

  24. PublicSharedallDoneAsNewManualResetEvent(False)  

  25.  

  26. 'Thisserverwaitsforaconnectionandthenusesasychronousoperationsto  

  27. 'accepttheconnection,getdatafromtheconnectedclient,  

  28. 'echothatdatabacktotheconnectedclient.  

  29. 'Itthendisconnectsfromtheclientandwaitsforanotherclient.  

  30. PublicSharedSubMain()  

  31. 'Databufferforincomingdata.  

  32. Dimbytes()AsByte=New[Byte](1023){}  

  33.  

  34. 'Establishthelocalendpointforthesocket.  

  35. DimipHostInfoAsIPHostEntry=DNS.Resolve(Dns.GetHostName())  

  36. DimipAddressAsIPAddress=ipHostInfo.AddressList(0)  

  37. DimlocalEndPointAsNewIPEndPoint(ipAddress,11000)  

  38.  

  39. 'CreateaTCP/IPsocket.  

  40. DimlistenerAsNewSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)  

  41.  

  42. 'Bindthesockettothelocalendpointandlistenforincomingconnections.  

  43. listener.Bind(localEndPoint)  

  44. listener.Listen(100)  

  45.  

  46. WhileTrue  

  47. 'Settheeventtononsignaledstate.  

  48. allDone.Reset()  

  49.  

  50. 'Startanasynchronoussockettolistenforconnections.  

  51. Console.WriteLine("Waitingforaconnection...")  

  52. listener.BeginAccept(NewAsyncCallback(AddressOfAcceptCallback),listener)  

  53.  

  54. 'Waituntilaconnectionismadeandprocessedbeforecontinuing.  

  55. allDone.WaitOne()  

  56. EndWhile  

  57. EndSub'Main  

  58.  

  59.  

  60. PublicSharedSubAcceptCallback(ByValarAsIAsyncResult)  

  61. 'Getthesocketthathandlestheclientrequest.  

  62. DimlistenerAsSocket=CType(ar.AsyncState,Socket)  

  63. 'Endtheoperation.  

  64. DimhandlerAsSocket=listener.EndAccept(ar)  

  65.  

  66. 'Createthestateobjectfortheasyncreceive.  

  67. DimstateAsNewStateObject  

  68. state.workSocket=handler 

  69. handler.BeginReceive(state.buffer,0,StateObject.
    BufferSize,0,NewAsyncCallback(AddressOfReadCallback),state)  

  70. EndSub'AcceptCallback  

  71.  

  72.  

  73. PublicSharedSubReadCallback(ByValarAsIAsyncResult)  

  74. DimcontentAsString=String.Empty  

  75.  

  76. 'Retrievethestateobjectandthehandlersocket  

  77. 'fromtheasynchronousstateobject.  

  78. DimstateAsStateObject=CType(ar.AsyncState,StateObject)  

  79. DimhandlerAsSocket=state.workSocket  

  80.  

  81. 'Readdatafromtheclientsocket.  

  82. DimbytesReadAsInteger=handler.EndReceive(ar)  

  83.  

  84. IfbytesRead>0Then  

  85. 'Theremightbemoredata,sostorethedatareceivedsofar.  

  86. state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead))  

  87.  

  88. 'Checkforend-of-filetag.Ifitisnotthere,read  

  89. 'moredata.  

  90. content=state.sb.ToString()  

  91. Ifcontent.IndexOf("<EOF>")>-1Then  

  92. 'Allthedatahasbeenreadfromthe  

  93. 'client.Displayitontheconsole.  

  94. Console.WriteLine("Read{0}bytesfromsocket."+vbLf+"Data:{1}",content.Length,content)  

  95. 'Echothedatabacktotheclient.  

  96. Send(handler,content)  

  97. Else  

  98. 'Notalldatareceived.Getmore.  

  99. handler.BeginReceive(state.buffer,0,StateObject.
    BufferSize,0,NewAsyncCallback(AddressOfReadCallback),state)  

  100. EndIf  

  101. EndIf  

  102. EndSub'ReadCallback  

  103.  

  104. PrivateSharedSubSend(ByValhandlerAsSocket,ByValdataAsString)  

  105. 'ConvertthestringdatatobytedatausingASCIIencoding.  

  106. DimbyteDataAsByte()=Encoding.ASCII.GetBytes(data)  

  107.  

  108. 'Beginsendingthedatatotheremotedevice.  

  109. handler.BeginSend(byteData,0,byteData.
    Length,0,NewAsyncCallback(AddressOfSendCallback),handler)  

  110. EndSub'Send  

  111.  

  112. PrivateSharedSubSendCallback(ByValarAsIAsyncResult)  

  113. 'Retrievethesocketfromthestateobject.  

  114. DimhandlerAsSocket=CType(ar.AsyncState,Socket)  

  115.  

  116. 'Completesendingthedatatotheremotedevice.  

  117. DimbytesSentAsInteger=handler.EndSend(ar)  

  118. Console.WriteLine("Sent{0}bytestoclient.",bytesSent)  

  119.  

  120. handler.Shutdown(SocketShutdown.Both)  

  121. handler.Close()  

  122. 'Signalthemainthreadtocontinue.  

  123. allDone.Set()  

  124. EndSub'SendCallback  

  125. EndClass'AsynchronousSocketListener 

看完了這篇文章,相信你對“VB.NET Socket編程的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

標(biāo)題名稱:VB.NETSocket編程的示例分析
轉(zhuǎn)載來于:http://www.rwnh.cn/article42/ihjjhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、做網(wǎng)站、關(guān)鍵詞優(yōu)化、服務(wù)器托管、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
永安市| 三明市| 台东市| 丁青县| 公主岭市| 灌云县| 石泉县| 昌黎县| 游戏| 大冶市| 北碚区| 浏阳市| 阆中市| 永和县| 武山县| 武陟县| 涞源县| 库车县| 元阳县| 定襄县| 明溪县| 吴江市| 邵阳县| 龙门县| 老河口市| 万全县| 陵水| 高要市| 华阴市| 安图县| 金坛市| 宁国市| 深水埗区| 集贤县| 昂仁县| 灵山县| 金寨县| 临夏市| 孟津县| 沁源县| 灵寿县|