F
fniles
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_OnRead
event did not get fired. How can I fix this problem ?
Thank you.
Imports SocketTools.SocketWrench.ErrorCode
Private WithEvents Socket As SocketTools.SocketWrench
Private Sub ServerForm_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) 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.SocketWrench
If Not Socket.IsInitialized Then
Throw New System.Exception("Unable to initialize
SocketWrench class")
End If
:
end sub
Private Sub ListenButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ListenButton.Click
If Socket.IsListening Then
Socket.Disconnect()
Else
Dim strLocalAddress As String
Dim nLocalPort As Integer
strLocalAddress = LocalAddress.Text.Trim()
nLocalPort = Val(LocalPort.Text)
Socket.Blocking = False
If Not Socket.Listen(strLocalAddress, nLocalPort) Then
exit sub
end if
end sub
Private Sub Socket_OnAccept(ByVal sender As Object, ByVal e As
SocketTools.SocketWrench.AcceptEventArgs) Handles Socket.OnAccept
Dim oSession As SessionClass
oSession = New SessionClass
oSession.Handle = e.Handle
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
oSession.ThreadMain), "CreateSocket")
end sub
Public Class SessionClass
Public WithEvents SocketClient As SocketTools.SocketWrench
Public Sub ThreadMain(ByVal data As Object)
Dim nResult As Integer
Dim Message As String
Message = CType(data, String)
If Message = "CreateSocket" Then
CreateSocket()
Else
nResult = SocketClient.Write(Message) '--> this one is send
out to cllient fine
End If
End Sub
Public Sub CreateSocket()
SocketClient = New SocketTools.SocketWrench
SocketClient.Initialize()
If Not SocketClient.Accept(Handle) Then
Exit Sub
End If
'AddHandler SocketClient.OnRead, AddressOf SocketClient_OnRead
--> even adding this does not help
End Sub
Private Sub SocketClient_OnRead(ByVal sender As Object, ByVal e As
System.EventArgs) Handles SocketClient.OnRead
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_OnRead
event did not get fired. How can I fix this problem ?
Thank you.
Imports SocketTools.SocketWrench.ErrorCode
Private WithEvents Socket As SocketTools.SocketWrench
Private Sub ServerForm_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) 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.SocketWrench
If Not Socket.IsInitialized Then
Throw New System.Exception("Unable to initialize
SocketWrench class")
End If
:
end sub
Private Sub ListenButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ListenButton.Click
If Socket.IsListening Then
Socket.Disconnect()
Else
Dim strLocalAddress As String
Dim nLocalPort As Integer
strLocalAddress = LocalAddress.Text.Trim()
nLocalPort = Val(LocalPort.Text)
Socket.Blocking = False
If Not Socket.Listen(strLocalAddress, nLocalPort) Then
exit sub
end if
end sub
Private Sub Socket_OnAccept(ByVal sender As Object, ByVal e As
SocketTools.SocketWrench.AcceptEventArgs) Handles Socket.OnAccept
Dim oSession As SessionClass
oSession = New SessionClass
oSession.Handle = e.Handle
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf
oSession.ThreadMain), "CreateSocket")
end sub
Public Class SessionClass
Public WithEvents SocketClient As SocketTools.SocketWrench
Public Sub ThreadMain(ByVal data As Object)
Dim nResult As Integer
Dim Message As String
Message = CType(data, String)
If Message = "CreateSocket" Then
CreateSocket()
Else
nResult = SocketClient.Write(Message) '--> this one is send
out to cllient fine
End If
End Sub
Public Sub CreateSocket()
SocketClient = New SocketTools.SocketWrench
SocketClient.Initialize()
If Not SocketClient.Accept(Handle) Then
Exit Sub
End If
'AddHandler SocketClient.OnRead, AddressOf SocketClient_OnRead
--> even adding this does not help
End Sub
Private Sub SocketClient_OnRead(ByVal sender As Object, ByVal e As
System.EventArgs) Handles SocketClient.OnRead
Dim x As Integer
x = 1 '---> this event never gets fired
End Sub