event of receive data on net work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qfchen
    New Member
    • Oct 2006
    • 91

    event of receive data on net work

    Since TCPClient in VB2005 does not implement data_received event like Winsock, how can I program an event routine to handle data_received event? Thanks.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Setup a thread to check to see if data is received?

    Comment

    • qfchen
      New Member
      • Oct 2006
      • 91

      #3
      So inside the thread, using Do-Loop to check sock.available? any sample code? thanks

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well you would want to have thread.sleep()s so you don't eat up the cpu time. But basically yes.
        You can even tell it to fire an event if you want.

        Comment

        • qfchen
          New Member
          • Oct 2006
          • 91

          #5
          Here is the piece of code which is running inside thread, not sure where to put thread.sleep(), how can it save CPU time. Thanks
          Public Sub start()
          Dim stream As NetworkStream
          Dim st As Boolean

          Dim cmd, parm As String
          Dim len, index As Integer
          Dim s As String = Nothing

          stream = Client.GetStrea m()
          Do
          st = stream.DataAvai lable

          If (st = True) Then
          Dim bytes(Client.Re ceiveBufferSize ) As Byte
          stream.Read(byt es, 0, CInt(Client.Rec eiveBufferSize) )
          s = Encoding.ASCII. GetString(bytes )
          len = s.Length
          index = s.IndexOf(":")
          If index < 0 Then
          Exit Sub
          End If

          'example CARD:1234567$
          cmd = s.Remove(index, len - index) 'keep CARD as cmd
          parm = s.Remove(0, index + 1) 'remove CARD:
          index = parm.IndexOf("$ ")
          Try
          parm = parm.Remove(ind ex, parm.Length - index)
          Catch ex As Exception
          End Try
          stn.ProcRXCmd(c md, parm)
          End If
          Loop
          End Sub

          Comment

          • qfchen
            New Member
            • Oct 2006
            • 91

            #6
            I think that I had figured out, just put the sleep() in side the Do-Loop. It works

            In my application, I use TCPClient to connect to multiple device, each device will have one thread running in my application to handle all activities asociate with that device, each device will send information to my application. My question is: If the info from all device come at the same time, will any data or string be lost, does TCPIP protocol take care and guarantee the traffic? Any setting needed for network to optimize the traffic?

            Thanks

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              I believe TCP should make sure you don't lose any data.

              Comment

              • qfchen
                New Member
                • Oct 2006
                • 91

                #8
                normally, in which situation, the network connection may lose? I notice the loss connection always happened during data transmission or after transmission. it must something happened during transmission. Any idea? Thanks.

                Comment

                Working...