Advertisement
Guest User

csock

a guest
Aug 18th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. Imports System
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.Text
  5.  
  6. Public Class StateObject
  7. Public workSocket As Socket = Nothing
  8. Public BufferSize As Integer = 32767
  9. Public buffer(32767) As Byte
  10. Public sb As New StringBuilder()
  11. End Class
  12.  
  13. Public Class SocketsClient
  14. Public Event onConnect()
  15. Public Event onError(ByVal Description As String)
  16. Public Event onDataArrival(ByVal Data As Byte(), ByVal TotalBytes As Integer)
  17. Public Event onDisconnect()
  18. Public Event onSendComplete(ByVal DataSize As Integer)
  19.  
  20. Private Shared response As [String] = [String].Empty
  21. Private Shared port As Integer = 44
  22. Private Shared ipHostInfo As IPHostEntry = Dns.GetHostEntry("localhost")
  23. Private Shared ipAddress As ipAddress = ipHostInfo.AddressList(0)
  24. Private Shared client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  25.  
  26. Public Sub Connect(ByVal RemoteHostName As String, ByVal RemotePort As Integer)
  27. Try
  28. client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  29. port = RemotePort
  30. ipHostInfo = Dns.GetHostEntry(RemoteHostName)
  31. ipAddress = ipHostInfo.AddressList(0)
  32. Dim remoteEP As New IPEndPoint(ipAddress, port)
  33. client.BeginConnect(remoteEP, AddressOf sockConnected, client)
  34. Catch
  35. RaiseEvent onError(Err.Description)
  36. Exit Sub
  37. End Try
  38. End Sub
  39.  
  40. Public Sub SendData(ByVal Data() As Byte)
  41. Try
  42. Dim byteData As Byte() = Data
  43. client.BeginSend(byteData, 0, byteData.Length, 0, AddressOf sockSendEnd, client)
  44. Catch
  45. RaiseEvent onError(Err.Description)
  46. Exit Sub
  47. End Try
  48. End Sub
  49.  
  50. Public Sub Disconnect()
  51. Try
  52. client.Shutdown(SocketShutdown.Both)
  53. Catch
  54. End Try
  55. client.Close()
  56. End Sub
  57.  
  58. Public Function StringToBytes(ByVal Data As String) As Byte()
  59. StringToBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
  60. End Function
  61.  
  62. Public Function BytestoString(ByVal Data As Byte()) As String
  63. BytestoString = System.Text.ASCIIEncoding.ASCII.GetString(Data)
  64. End Function
  65.  
  66. Private Sub sockConnected(ByVal ar As IAsyncResult)
  67. Try
  68. If client.Connected = False Then RaiseEvent onError("Connection refused.") : Exit Sub
  69. Dim state As New StateObject()
  70. state.workSocket = client
  71. client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
  72. RaiseEvent onConnect()
  73. Catch
  74. RaiseEvent onError(Err.Description)
  75. Exit Sub
  76. End Try
  77. End Sub
  78.  
  79. Private Sub sockDataArrival(ByVal ar As IAsyncResult)
  80. Dim state As StateObject = CType(ar.AsyncState, StateObject)
  81. Dim client As Socket = state.workSocket
  82. Dim bytesRead As Integer
  83.  
  84. Try
  85. bytesRead = client.EndReceive(ar)
  86. Catch
  87. Exit Sub
  88. End Try
  89.  
  90. Try
  91. Dim Data() As Byte = state.buffer
  92. If bytesRead = 0 Then
  93. client.Shutdown(SocketShutdown.Both)
  94. client.Close()
  95. RaiseEvent onDisconnect()
  96. Exit Sub
  97. End If
  98. ReDim state.buffer(32767)
  99.  
  100. client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf sockDataArrival, state)
  101. RaiseEvent onDataArrival(Data, bytesRead)
  102. Catch
  103. RaiseEvent onError(Err.Description)
  104. Exit Sub
  105. End Try
  106. End Sub
  107.  
  108. Private Sub sockSendEnd(ByVal ar As IAsyncResult)
  109. Try
  110. Dim client As Socket = CType(ar.AsyncState, Socket)
  111. Dim bytesSent As Integer = client.EndSend(ar)
  112. RaiseEvent onSendComplete(bytesSent)
  113. Catch
  114. RaiseEvent onError(Err.Description)
  115. Exit Sub
  116. End Try
  117. End Sub
  118.  
  119. Public Function Connected() As Boolean
  120. Try
  121. Return client.Connected
  122. Catch
  123. RaiseEvent onError(Err.Description)
  124. Exit Function
  125. End Try
  126. End Function
  127. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement