I am using ThreadPool to call a sub in a class. The first time the
ThreadPool is called, I created a socket in the thread. I can connect a
client to the socket and send a message to the client (in ThreadMain),
but when the client send a message to me, the Sub SocketClient_On Read
event did not get fired. How can I fix this problem ?
Thank you.
Imports SocketTools.Soc ketWrench.Error Code
Private WithEvents Socket As SocketTools.Soc ketWrench
Private Sub ServerForm_Load (ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles MyBase.Load
Dim nIndex As Integer
' Create an instance of the SocketWrench class which will
' function as our listening (server) socket
Socket = New SocketTools.Soc ketWrench
If Not Socket.IsInitia lized Then
Throw New System.Exceptio n("Unable to initialize
SocketWrench class")
End If
:
end sub
Private Sub ListenButton_Cl ick(ByVal sender As System.Object, ByVal
e As System.EventArg s) Handles ListenButton.Cl ick
If Socket.IsListen ing Then
Socket.Disconne ct()
Else
Dim strLocalAddress As String
Dim nLocalPort As Integer
strLocalAddress = LocalAddress.Te xt.Trim()
nLocalPort = Val(LocalPort.T ext)
Socket.Blocking = False
If Not Socket.Listen(s trLocalAddress, nLocalPort) Then
exit sub
end if
end sub
Private Sub Socket_OnAccept (ByVal sender As Object, ByVal e As
SocketTools.Soc ketWrench.Accep tEventArgs) Handles Socket.OnAccept
Dim oSession As SessionClass
oSession = New SessionClass
oSession.Handle = e.Handle
ThreadPool.Queu eUserWorkItem(N ew WaitCallback(Ad dressOf
oSession.Thread Main), "CreateSock et")
end sub
Public Class SessionClass
Public WithEvents SocketClient As SocketTools.Soc ketWrench
Public Sub ThreadMain(ByVa l data As Object)
Dim nResult As Integer
Dim Message As String
Message = CType(data, String)
If Message = "CreateSock et" Then
CreateSocket()
Else
nResult = SocketClient.Wr ite(Message) '--> this one is send
out to cllient fine
End If
End Sub
Public Sub CreateSocket()
SocketClient = New SocketTools.Soc ketWrench
SocketClient.In itialize()
If Not SocketClient.Ac cept(Handle) Then
Exit Sub
End If
'AddHandler SocketClient.On Read, AddressOf SocketClient_On Read
--> even adding this does not help
End Sub
Private Sub SocketClient_On Read(ByVal sender As Object, ByVal e As
System.EventArg s) Handles SocketClient.On Read
Dim x As Integer
x = 1 '---> this event never gets fired
End Sub
ThreadPool is called, I created a socket in the thread. I can connect a
client to the socket and send a message to the client (in ThreadMain),
but when the client send a message to me, the Sub SocketClient_On Read
event did not get fired. How can I fix this problem ?
Thank you.
Imports SocketTools.Soc ketWrench.Error Code
Private WithEvents Socket As SocketTools.Soc ketWrench
Private Sub ServerForm_Load (ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles MyBase.Load
Dim nIndex As Integer
' Create an instance of the SocketWrench class which will
' function as our listening (server) socket
Socket = New SocketTools.Soc ketWrench
If Not Socket.IsInitia lized Then
Throw New System.Exceptio n("Unable to initialize
SocketWrench class")
End If
:
end sub
Private Sub ListenButton_Cl ick(ByVal sender As System.Object, ByVal
e As System.EventArg s) Handles ListenButton.Cl ick
If Socket.IsListen ing Then
Socket.Disconne ct()
Else
Dim strLocalAddress As String
Dim nLocalPort As Integer
strLocalAddress = LocalAddress.Te xt.Trim()
nLocalPort = Val(LocalPort.T ext)
Socket.Blocking = False
If Not Socket.Listen(s trLocalAddress, nLocalPort) Then
exit sub
end if
end sub
Private Sub Socket_OnAccept (ByVal sender As Object, ByVal e As
SocketTools.Soc ketWrench.Accep tEventArgs) Handles Socket.OnAccept
Dim oSession As SessionClass
oSession = New SessionClass
oSession.Handle = e.Handle
ThreadPool.Queu eUserWorkItem(N ew WaitCallback(Ad dressOf
oSession.Thread Main), "CreateSock et")
end sub
Public Class SessionClass
Public WithEvents SocketClient As SocketTools.Soc ketWrench
Public Sub ThreadMain(ByVa l data As Object)
Dim nResult As Integer
Dim Message As String
Message = CType(data, String)
If Message = "CreateSock et" Then
CreateSocket()
Else
nResult = SocketClient.Wr ite(Message) '--> this one is send
out to cllient fine
End If
End Sub
Public Sub CreateSocket()
SocketClient = New SocketTools.Soc ketWrench
SocketClient.In itialize()
If Not SocketClient.Ac cept(Handle) Then
Exit Sub
End If
'AddHandler SocketClient.On Read, AddressOf SocketClient_On Read
--> even adding this does not help
End Sub
Private Sub SocketClient_On Read(ByVal sender As Object, ByVal e As
System.EventArg s) Handles SocketClient.On Read
Dim x As Integer
x = 1 '---> this event never gets fired
End Sub
Comment