Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.72 KB | None | 0 0
  1. Imports System.Threading
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.IO
  5. Public Class Client
  6.  
  7.     Private client As TcpClient
  8.     Private netStream As NetworkStream
  9.     Public Shared Property incommingBytes As Long = 0
  10.     Public Shared Property outcommingBytes As Long = 0
  11.     Private Shared incommingBytesLock As New Object
  12.     Private Shared outcommingBytesLock As New Object
  13.  
  14.  
  15.     Public ReadOnly Property remoteIp As String = String.Empty
  16.  
  17.     Public Event IncommingData(ByRef c As Client, ByVal header As Byte, ByVal buffer() As Byte)
  18.     Public Event Disconnected(ByRef c As Client)
  19.  
  20.     Sub New(ByRef c As TcpClient)
  21.         client = c
  22.         Try
  23.  
  24.             remoteIp = c.Client.RemoteEndPoint.ToString
  25.             netStream = client.GetStream()
  26.             netStream.BeginRead(New Byte() {0}, 0, 0, AddressOf ReadAsync, Nothing)
  27.  
  28.         Catch ex As Exception
  29.             RaiseEvent Disconnected(Me)
  30.         End Try
  31.  
  32.     End Sub
  33.  
  34.  
  35.     Private Sub ReadAsync(ByVal iAR As IAsyncResult)
  36.         Try
  37.            'You read here
  38.             RaiseEvent IncommingData(Me, header, buffer)
  39.             netStream.BeginRead(New Byte() {0}, 0, 0, AddressOf ReadAsync, Nothing)
  40.         Catch ex As Exception
  41.             RaiseEvent Disconnected(Me)
  42.         End Try
  43.  
  44.     End Sub
  45.  
  46.     Public Sub SendAsync(ByVal header As Byte)
  47.         Try
  48.             'You write here
  49.         Catch ex As Exception
  50.             RaiseEvent Disconnected(Me)
  51.         End Try
  52.  
  53.     End Sub
  54.  
  55.     Public Sub SendAsync(ByVal header As Byte, ByVal buffer() As Byte)
  56.         Try
  57.             'You write here
  58.            
  59.         Catch ex As Exception
  60.             RaiseEvent Disconnected(Me)
  61.         End Try
  62.  
  63.     End Sub
  64.  
  65. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement