Yea a reply
Here is the code from the lister class.
Private Sub Listen_Callback(ByVal ar As IAsyncResult)
Dim t As New Client(_lSocket.EndAccept(ar))
t.Connect()
_lSocket.BeginAccept(New AsyncCallback(AddressOf Listen_Callback),
_lSocket)
End Sub
Here is the code for the client..
i only implement so i can get the dispose.
Based on "_Do_Connect()" is the class threaded?.. also do I really need
threading? I mean I did this class without the threading. and had around
2,399 connections and the respons was good. Note: the raiseevents are not
in, but the one that did the 2399 connections was sending around 1k of junk
data.
Public Class Client
implements iClient
Private _Socket As Net.Sockets.Socket
Private _RecvBuffer(1024) As Byte
Private _SendBuffer(1024) As Byte
Private _ID As String = Guid.NewGuid.ToString
Private _recvMsg As String
Private _Timer As Threading.Timer
Private _MyThread As Threading.Thread
Private _ShutDown As Boolean
Public Event Disconnected(ByVal ID As String) Implements
iClient.Disconnected
Public Event Connected(ByVal ID As String) Implements iClient.Connected
Public Event Data(ByVal ID As String, ByVal Data As String) Implements
iClient.Data
Public Property Socket() As System.Net.Sockets.Socket Implements
iClient.Socket
Get
Return _Socket
End Get
Set(ByVal Value As System.Net.Sockets.Socket)
_Socket = Value
End Set
End Property
'
Public Property ID() As String Implements iClient.ID
Get
Return _ID
End Get
Set(ByVal Value As String)
End Set
End Property
'
Private Sub _Do_recv(ByVal ar As System.IAsyncResult)
Try
Dim read As Integer = _Socket.EndReceive(ar)
_recvMsg = System.Text.Encoding.ASCII.GetString(_RecvBuffer, 0,
read)
_Socket.BeginReceive(_RecvBuffer, 0, 1024, 0, New
AsyncCallback(AddressOf _Do_recv), _Socket)
Catch ex As Exception
Me.Disconnect()
End Try
End Sub
'
Private Sub _Do_Send(ByVal ar As System.IAsyncResult)
Try
Dim tmpSend As Int16 = _Socket.EndSend(ar)
Catch ex As Exception
Me.Disconnect()
End Try
End Sub
Private Sub _Do_CheckUp(ByVal state As Object)
Try
Dim tmpSendBytes() As Byte = {0}
_Socket.Send(tmpSendBytes)
If _Socket.Poll(1000, Net.Sockets.SelectMode.SelectWrite) =
False Then
Me.Disconnect()
End If
Catch ex As Exception
Me.Disconnect()
End Try
End Sub
Private Sub _Do_Connect()
Try
_Socket.Blocking = False
_Socket.BeginReceive(_RecvBuffer, 0, 1024, 0, New
AsyncCallback(AddressOf _Do_recv), _Socket)
_Timer = New Threading.Timer(New
Threading.TimerCallback(AddressOf _Do_CheckUp), Nothing, 5000, 5000)
Me.Send("Hello!")
Do While _ShutDown = False
_MyThread.Sleep(100)
Loop
Catch ex As Exception
Me.Disconnect()
End Try
End Sub
'
Public Sub Connect() Implements iClient.Connect
Try
_MyThread = New Threading.Thread(AddressOf _Do_Connect)
_MyThread.Start()
Catch ex As Exception
Me.Disconnect()
End Try
End Sub
Public Sub Disconnect() Implements iClient.Disconnect
Me.Dispose()
End Sub
'
Public Sub Send(ByVal Data As String) Implements iClient.Send
Try
Dim tmpData() As Byte = System.Text.Encoding.ASCII.GetBytes(Data)
_Socket.BeginSend(tmpData, 0, tmpData.Length, 0, New
AsyncCallback(AddressOf _Do_Send), _Socket)
Catch ex As Exception
Me.Disconnect()
End Try
End Sub
'
Public Sub Dispose() Implements System.IDisposable.Dispose
Try
_Timer.Dispose()
_ShutDown = True
_MyThread.Abort()
_Socket.Shutdown(Net.Sockets.SocketShutdown.Both)
_Socket.Close()
CloseHandle(_Socket.Handle)
_Socket = Nothing
' GC.SuppressFinalize(Me)
Catch ex As Exception
End Try
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
' Use interop to call the method necessary
' to clean up the unmanaged resource.
<System.Runtime.InteropServices.DllImport("Kernel32")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
End Function
Public Sub New(ByVal Sock As Net.Sockets.Socket)
_Socket = Sock
End Sub
End Class