I know this needs more work but any comments on what I need to add, to change, to improve would be helpful. I know the error handling has to be improved drastically.
Do I need to implement dispose? What have I missed?
Or should I just give up now and take up fishing?
Code:
Imports System.Net
Imports System.Net.Sockets
Public Class Listener
Inherits TcpListener
Public Event NewClientEvent As EventHandler(Of NewClientEventArgs)
Public Event ErrorEvent As EventHandler(Of ListenerErrorEventArgs)
Public Sub New(ByVal EPoint As IPEndPoint)
MyBase.New(EPoint)
StartListening()
End Sub
Private Sub listenerCallback(ByVal ar As IAsyncResult)
Try
Dim lstnr As Listener = DirectCast(ar.AsyncState, Listener)
Dim client As TcpClient = lstnr.EndAcceptTcpClient(ar)
Me.BeginAcceptTcpClient(New AsyncCallback(AddressOf listenerCallback), Me)
OnNewClient(client)
Catch ex As Exception
OnError(ex)
End Try
End Sub
Private Sub OnError(ByVal ex As Exception)
If Me.Active Then
Me.Stop()
End If
Dim ListnError As New ListenerErrorEventArgs
ListnError.Ex = ex
RaiseEvent ErrorEvent(Me, ListnError)
End Sub
Private Sub OnNewClient(ByVal client As TcpClient)
Dim newClientArgs As New NewClientEventArgs
newClientArgs.client = client
RaiseEvent NewClientEvent(Me, newClientArgs)
End Sub
Public Sub StartListening()
Try
If Not Me.Active Then
Me.Start()
Me.BeginAcceptTcpClient(New AsyncCallback(AddressOf listenerCallback), Me)
End If
Catch ex As Exception
OnError(ex)
End Try
End Sub
End Class
Or should I just give up now and take up fishing?