Socket and ThreadPool

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
 
T

Tom Shelton

fniles said:
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

I really don't know Catalysts stuff very well... But, I have to wonder
why you would want to use a 3rd party library for this? Does this
provide features, that can't be found in the System.Net and
System.Net.Sockets namespaces?

I'm trying to think of some questions that might unravel this a bit...
The only things right now I can think of is what version of this
control is it? Is it a .NET control or a COM control? I am vaguely
uncomfortable with the structure of your code - but I would need to do
a little research to verify...
 
F

fniles

Thanks.
The reason why we chose 3rd party (it is a .NET Component) because in VB6
Microsoft Winsock control is slower than a 3rd party control.
What .NET socket component that I can use that has good performance ?

Thanks.
 
M

Michael D. Ober

Take a look at the native system.net.socket classes. They are quick and
powerful. I was able to use these classes to replace the WinINET and
MSWinSck.OCX interfaces that I used in VB 6.

Mike Ober.

fniles said:
Thanks.
The reason why we chose 3rd party (it is a .NET Component) because in VB6
Microsoft Winsock control is slower than a 3rd party control.
What .NET socket component that I can use that has good performance ?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top