Corayzon Regular Posts: 67
|
Code: | Public Class AsyncSocketServer Public Sub New() allDone = New System.Threading.ManualResetEvent(False) End Sub Public Class clsClientObject Private bConnected As Boolean = True Public Socket As System.Net.Sockets.Socket = Nothing Public Buffer(1024) As Byte Public Tag As Object Public Property Connected() As Boolean Get Return bConnected End Get Set(ByVal Value As Boolean) bConnected = Value End Set End Property Public Sub Send(ByVal sData As String) Try Me.Socket.Send(System.Text.Encoding.ASCII.GetBytes(sData)) Catch ex As Exception Me.Disconnect() End Try End Sub Public Sub Send(ByVal bData() As Byte) Try Me.Socket.Send(bData) Catch ex As Exception Me.Disconnect() End Try End Sub Public Sub Disconnect() Try Socket.Shutdown(Net.Sockets.SocketShutdown.Both) Socket.Close() Catch ex As Exception End Try Socket = Nothing bConnected = False End Sub End Class Protected bListening As Boolean = False Protected allDone As System.Threading.ManualResetEvent Protected iPort As Integer Protected sHost As String Protected Listener As System.Net.sockets.Socket Private Sub Listen() Try Listener = New System.Net.sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, _ System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp) Dim ipHostInfo As System.Net.IPHostEntry = System.Net.Dns.Resolve(sHost) Dim ipAddress As System.Net.IPAddress = ipHostInfo.AddressList(0) Dim localEndPoint As New System.Net.IPEndPoint(ipAddress, iPort) Listener.Bind(localEndPoint) Listener.Listen(100) RaiseEvent ServerStarted() bListening = True While bListening = True allDone.Reset() Try Listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), Listener) Catch ex As Exception RaiseEvent SocketError("Listen() :: This could be because of routine Close() process", ex) End Try allDone.WaitOne() End While Listener.Close() RaiseEvent ServerStopped() Catch ex As Exception RaiseEvent SocketError("Listen", ex) End Try End Sub Public Sub Listen(ByVal sHostName As String, ByVal iHostPort As Integer) sHost = sHostName iPort = iHostPort Dim Thread As New System.Threading.Thread(AddressOf Listen) Thread.Start() End Sub Public Sub Listen(ByVal sHostName As String, ByVal sHostPort As String) sHost = sHostName iPort = Int(sHostPort) Dim Thread As New System.Threading.Thread(AddressOf Listen) Thread.Start() End Sub Public Sub Close() Try bListening = False allDone.Set() Catch ex As Exception RaiseEvent SocketError("Close()", ex) End Try End Sub Private Sub AcceptCallback(ByVal AsyncResult As IAsyncResult) ' Signal the main thread to continue. allDone.Set() ' Get the socket that handles the connection request. Dim Listener As System.Net.Sockets.Socket = AsyncResult.AsyncState Dim Client As New clsClientObject Try Client.Socket = Listener.EndAccept(AsyncResult) Catch ex As Exception Client.Disconnect() RaiseEvent SocketClosed(Client) Client = Nothing Exit Sub End Try ' Call Connection RaiseEvent SocketConnected(Client) ' Check for connection If Client.Connected = False Then RaiseEvent SocketClosed(Client) Client = Nothing Exit Sub End If ' Wait for data Try Client.Socket.BeginReceive(Client.Buffer, 0, 1024, Net.Sockets.SocketFlags.None, New AsyncCallback(AddressOf ReadCallback), Client) Catch ex As Exception Client.Disconnect() RaiseEvent SocketClosed(Client) Client = Nothing End Try End Sub Private Sub ReadCallback(ByVal AsyncResult As IAsyncResult) Try Dim Client As clsClientObject = AsyncResult.AsyncState Dim bytesRead As Integer = 0 Try bytesRead = Client.Socket.EndReceive(AsyncResult) ' Check if socket was disconnected If bytesRead = 0 Then Client.Disconnect() RaiseEvent SocketClosed(Client) Client = Nothing Exit Sub End If Catch Client.Disconnect() RaiseEvent SocketClosed(Client) Client = Nothing Exit Sub End Try ' call data arrival RaiseEvent DataArrival(Client, Client.Buffer) ' check for disconnection If Client.Connected = False Then RaiseEvent SocketClosed(Client) Client = Nothing Exit Sub End If ' clear buffer and wait for data Dim Bytes(1023) As Byte Client.Buffer = Bytes Try Client.Socket.BeginReceive(Client.Buffer, 0, 1024, 0, New AsyncCallback(AddressOf ReadCallback), Client) Catch ex As Exception Client.Disconnect() RaiseEvent SocketClosed(Client) Client = Nothing End Try Catch ex As Exception RaiseEvent SocketError("ReadCallBack()", ex) End Try End Sub Public Event SocketConnected(ByRef Client As clsClientObject) Public Event SocketClosed(ByVal Client As clsClientObject) Public Event DataArrival(ByRef Client As clsClientObject, ByVal bData() As Byte) Public Event SocketError(ByVal sSource As String, ByVal Ex As System.Exception) Public Event ServerStopped() Public Event ServerStarted() End Class | For help on how to use this class u can pop a reply to this post here =]
|