Home All Groups Group Topic Archive Search About

Socket and ThreadPool

Author
31 May 2006 10:05 PM
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

Author
31 May 2006 10:39 PM
Tom Shelton
fniles wrote:
Show quoteHide quote
> 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...

--
Tom Shelton [MVP]
Author
4 Jun 2006 10:53 AM
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.

Show quoteHide quote
"Tom Shelton" <t**@mtogden.com> wrote in message
news:1149115189.255303.245290@f6g2000cwb.googlegroups.com...
>
> fniles wrote:
>> 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...
>
> --
> Tom Shelton [MVP]
>
Author
6 Jun 2006 1:41 AM
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.

Show quoteHide quote
"fniles" <fni***@pfmail.com> wrote in message
news:OAXme6IiGHA.1508@TK2MSFTNGP04.phx.gbl...
> 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.
>
> "Tom Shelton" <t**@mtogden.com> wrote in message
> news:1149115189.255303.245290@f6g2000cwb.googlegroups.com...
> >
> > fniles wrote:
> >> 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...
> >
> > --
> > Tom Shelton [MVP]
> >
>
>
>