成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

VB.NET Socket編程實(shí)際操作方法介紹

開發(fā) 后端
我們今天就通過一段基于服務(wù)器中的代碼實(shí)現(xiàn)來為大家詳細(xì)講解有關(guān)VB.NET Socket編程的相關(guān)操作方法,希望能對(duì)又需要的朋友有所幫助。

 VB.NET 應(yīng)用范圍非常廣泛。我們可以在VB.NET環(huán)境中進(jìn)行一些特定語言的編寫。下面就來看一下VB.NET Socket編程的相關(guān)方法。一直以來很想學(xué)習(xí)Socket編程方面的應(yīng)用,比如怎樣通過Socket編程實(shí)現(xiàn)單片機(jī)與PC的TCP連接通信。在單片機(jī)嵌入網(wǎng)卡芯片與PC進(jìn)行連接通信,實(shí)現(xiàn)PC的web方式對(duì)單片機(jī)所控制的設(shè)備的狀態(tài)管理,例如智能家居方面的應(yīng)用。#t#

下面通過例子來學(xué)習(xí)VB.NET Socket編程類的應(yīng)用,下面的程序是服務(wù)器中的代碼實(shí)現(xiàn):

Visual Basic ---tcpserver

  1. Imports System  
  2. Imports System.Net  
  3. Imports System.Net.Sockets  
  4. Imports System.Text  
  5. Imports System.Threading  
  6. Imports Microsoft.VisualBasic  
  7. ' State object for reading client 
    data asynchronously  
  8. Public Class StateObject  
  9. ' Client socket.  
  10. Public workSocket As Socket = Nothing 
  11. ' Size of receive buffer.  
  12. Public Const BufferSize As Integer = 1024 
  13. ' Receive buffer.  
  14. Public buffer(BufferSize) As Byte  
  15. ' Received data string.  
  16. Public sb As New StringBuilder  
  17. End Class 'StateObject  
  18. Public Class AsynchronousSocket
    Listener  
  19. ' Thread signal.  
  20. Public Shared allDone As New Manual
    ResetEvent(False)  
  21. ' This server waits for a connection 
    and then uses asychronous operations to  
  22. ' accept the connection, get data from 
    the connected client,   
  23. ' echo that data back to the 
    connected client.  
  24. ' It then disconnects from the 
    client and waits for another client.   
  25. Public Shared Sub Main()  
  26. ' Data buffer for incoming data.  
  27. Dim bytes() As Byte = New [Byte](1023) {}  
  28. ' Establish the local endpoint for the socket.  
  29. Dim ipHostInfo As IPHostEntry = 
    Dns.Resolve(Dns.GetHostName())  
  30. Dim ipAddress As IPAddress = 
    ipHostInfo.AddressList(0)  
  31. Dim localEndPoint As New IPEndPoint
    (ipAddress, 11000)  
  32. ' Create a TCP/IP socket.  
  33. Dim listener As New Socket(AddressFamily.
    InterNetwork, SocketType.Stream, ProtocolType.Tcp)  
  34. ' Bind the socket to the local endpoint 
    and listen for incoming connections.  
  35. listener.Bind(localEndPoint)  
  36. listener.Listen(100)  
  37. While True  
  38. ' Set the event to nonsignaled state.  
  39. allDone.Reset()  
  40. ' Start an asynchronous socket to listen 
    for connections.  
  41. Console.WriteLine("Waiting for a connection...")  
  42. listener.BeginAccept(New AsyncCallback
    (AddressOf AcceptCallback), listener)  
  43. ' Wait until a connection is made and 
    processed before continuing.  
  44. allDone.WaitOne()  
  45. End While  
  46. End Sub 'Main  
  47. Public Shared Sub AcceptCallback(ByVal ar 
    As IAsyncResult)  
  48. ' Get the socket that handles the client request.  
  49. Dim listener As Socket = CType(ar.AsyncState, Socket)  
  50. ' End the operation.  
  51. Dim handler As Socket = listener.EndAccept(ar)  
  52. ' Create the state object for the async receive.  
  53. Dim state As New StateObject  
  54. state.workSocket = handler 
  55. handler.BeginReceive(state.buffer, 0, StateObject.
    BufferSize, 0, New AsyncCallback(AddressOf 
    ReadCallback), state)  
  56. End Sub 'AcceptCallback  
  57. Public Shared Sub ReadCallback(ByVal ar As 
    IAsyncResult)  
  58. Dim content As StringString = String.Empty  
  59. ' Retrieve the state object and the handler socket  
  60. ' from the asynchronous state object.  
  61. Dim state As StateObject = CType(ar.AsyncState, 
    StateObject)  
  62. Dim handler As Socket = state.workSocket  
  63. ' Read data from the client socket.   
  64. Dim bytesRead As Integer = handler.EndReceive(ar)  
  65. If bytesRead > 0 Then  
  66. ' There might be more data, so store the data 
    received so far.  
  67. state.sb.Append(Encoding.ASCII.GetString
    (state.buffer, 0, bytesRead))  
  68. ' Check for end-of-file tag. If it is not there, read   
  69. ' more data.  
  70. content = state.sb.ToString()  
  71. If content.IndexOf("<EOF>") > -1 Then  
  72. ' All the data has been read from the   
  73. ' client. Display it on the console.  
  74. Console.WriteLine("Read {0} bytes from socket. "
     + vbLf + " Data : {1}", content.Length, content)  
  75. ' Echo the data back to the client.  
  76. Send(handler, content)  
  77. Else  
  78. ' Not all data received. Get more.  
  79. handler.BeginReceive(state.buffer, 0, StateObject.
    BufferSize, 0, New AsyncCallback(AddressOf 
    ReadCallback), state)  
  80. End If  
  81. End If  
  82. End Sub 'ReadCallback  
  83. Private Shared Sub Send(ByVal handler As Socket, 
    ByVal data As String)  
  84. ' Convert the string data to byte data using 
    ASCII encoding.  
  85. Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)  
  86. ' Begin sending the data to the remote device.  
  87. handler.BeginSend(byteData, 0, byteData.Length, 0, 
    New AsyncCallback(AddressOf SendCallback), handler)  
  88. End Sub 'Send  
  89. Private Shared Sub SendCallback(ByVal ar As IAsyncResult)  
  90. ' Retrieve the socket from the state object.  
  91. Dim handler As Socket = CType(ar.AsyncState, Socket)  
  92. ' Complete sending the data to the remote device.  
  93. Dim bytesSent As Integer = handler.EndSend(ar)  
  94. Console.WriteLine("Sent {0} bytes to client.", bytesSent)  
  95. handler.Shutdown(SocketShutdown.Both)  
  96. handler.Close()  
  97. ' Signal the main thread to continue.  
  98. allDone.Set()  
  99. End Sub 'SendCallback  
  100. End Class 'AsynchronousSocketListener 

 

VB.NET Socket編程的具體用法就為大家介紹到這里。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-01-07 18:05:18

VB.NET事務(wù)處理

2009-12-30 15:53:28

Silverlight

2010-01-11 10:19:18

VB.NET啟動(dòng)外部程

2010-01-14 14:46:57

2010-01-07 18:17:00

VB.NET連接SAP

2010-01-11 15:43:06

VB.NET類屬性

2010-01-07 10:28:04

VB.NET實(shí)現(xiàn)接口

2010-01-11 10:34:41

VB.NET圖像操作

2010-01-04 16:50:04

Silverlight

2010-01-21 15:56:31

VB.NET文本框

2010-01-07 11:07:20

VB.NET讀取INI

2010-01-07 15:37:35

VB.NET ForNext循環(huán)

2009-10-23 17:22:48

VB.NET編程

2010-01-07 15:25:11

VB.NET數(shù)組

2010-01-22 10:41:33

VB.NET聲明結(jié)構(gòu)

2009-11-10 12:42:47

VB.NET Prin

2010-01-11 11:37:08

VB.NET操作CSV

2010-02-03 10:23:47

C++操作符重載

2010-01-15 18:12:28

VB.NET超鏈接

2010-01-15 19:17:23

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产综合在线视频 | 国产三级精品三级在线观看四季网 | 久久精品国产免费 | 久久久精品一区 | 一级片在线免费看 | 人人玩人人添人人澡欧美 | 欧美精品影院 | 午夜影院在线观看免费 | 国产综合精品一区二区三区 | 久久狼人天堂 | 久久视频精品 | www日日日| 亚洲福利一区二区 | 99久久婷婷国产亚洲终合精品 | 国产成人免费视频网站视频社区 | 91免费福利视频 | 国产在线精品一区二区 | 波多野结衣在线观看一区二区三区 | 日韩欧美一级片 | 国产一区免费视频 | 狠狠躁18三区二区一区 | 国产午夜精品一区二区三区 | 免费在线看黄 | 欧美午夜影院 | 亚洲第一成人av | 欧美色偷拍 | av网站免费观看 | 欧美一区二区黄 | 玖玖爱365 | 亚洲国产成人精品女人久久久 | 日韩亚洲欧美一区 | 亚洲综合一区二区三区 | 91精品国产91久久久久久不卞 | 亚洲一区二区三区在线免费 | 欧美日韩在线免费观看 | 中文字幕一区二区三区在线乱码 | 免费一看一级毛片 | 97人人澡人人爽91综合色 | 亚洲最大的成人网 | 久久久精彩视频 | 狠狠操天天操 |