Socket and ThreadPool

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fniles

    Socket and ThreadPool

    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

  • Tom Shelton

    #2
    Re: Socket and ThreadPool


    fniles wrote:[color=blue]
    > 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[/color]

    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.Sock ets 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]

    Comment

    • fniles

      #3
      Re: Socket and ThreadPool

      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" <tom@mtogden.co m> wrote in message
      news:1149115189 .255303.245290@ f6g2000cwb.goog legroups.com...[color=blue]
      >
      > fniles wrote:[color=green]
      >> 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[/color]
      >
      > 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.Sock ets 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]
      >[/color]


      Comment

      • Michael D. Ober

        #4
        Re: Socket and ThreadPool

        Take a look at the native system.net.sock et 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" <fniles@pfmail. com> wrote in message
        news:OAXme6IiGH A.1508@TK2MSFTN GP04.phx.gbl...[color=blue]
        > 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" <tom@mtogden.co m> wrote in message
        > news:1149115189 .255303.245290@ f6g2000cwb.goog legroups.com...[color=green]
        > >
        > > fniles wrote:[color=darkred]
        > >> 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[/color]
        > >
        > > 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.Sock ets 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]
        > >[/color]
        >
        >
        >[/color]



        Comment

        Working...