自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手把手指導VB.NET Socket編程

開發(fā) 后端
這里介紹一直以來很想學習VB.NET Socket編程方面的應用,比如怎樣通過VB.NET Socket編程實現(xiàn)單片機與PC的TCP連接通信。

VB.NET經過長時間的發(fā)展,很多用戶都很了解VB.NET Socket編程了,這里我發(fā)表一下個人理解,和大家討論討論。一直以來很想學習VB.NET Socket編程方面的應用,比如怎樣通過VB.NET Socket編程實現(xiàn)單片機與PC的TCP連接通信。在單片機嵌入網卡芯片與PC進行連接通信,實現(xiàn)PC的web方式對單片機所控制的設備的狀態(tài)管理,例如智能家居方面的應用。

下面通過例子來學習VB.NET Socket編程類的應用,下面的程序分別分服務器和客戶端兩部分:

  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 

【編輯推薦】

  1. 詳細分析VB.NET WithEvents
  2. 淺析VB.NET局部靜態(tài)變量
  3. 原理分析VB.NET開發(fā)控件
  4. 自己動手用代碼實現(xiàn)VB.NET ListView加載數(shù)據(jù)
  5. 詳細介紹VB.NET MyClass
責任編輯:佚名 來源: IT168
相關推薦

2009-11-02 15:33:53

VB.NET Data

2009-10-22 15:23:32

VB.NET函數(shù)

2009-10-27 16:05:52

VB.NET File

2014-02-27 10:27:46

PC遠程維護

2021-12-15 14:39:50

LinuxGPG加解密文件

2021-08-04 08:55:02

Socket Java開發(fā)

2010-01-07 10:46:27

VB.NET Sock

2010-08-17 14:29:15

2009-10-23 17:03:18

VB.NET事件編程

2011-01-06 10:39:25

.NET程序打包

2015-07-28 14:27:44

2010-01-11 18:12:57

VB.NET使用MS

2009-11-02 15:16:07

VB.NET編程

2010-01-14 17:11:17

VB.NET枚舉

2009-11-02 15:08:58

VB.NET Obje

2009-10-14 15:34:29

VB.NET窗體編程模

2009-11-10 13:08:13

VB.NET編程技巧

2009-11-02 14:55:52

VB.NET Obje

2009-11-10 15:30:46

VB.NET編程原則

2009-10-29 11:41:27

VB.NET寫Obje
點贊
收藏

51CTO技術棧公眾號