Merging data received via TCP

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

    #1

    Merging data received via TCP

    Hi,

    I'm having a weird problem... Part of code first:

    Dim bytes(tcpClient .ReceiveBufferS ize) As Byte
    Dim DataChunk as String
    Dim FullData as String

    Do While EOF = False
    If networkStream.D ataAvailable = True Then
    networkStream.R ead(bytes, 0, CInt(tcpClient. ReceiveBufferSi ze))
    DataChunk = Encoding.ASCII. GetString(bytes )
    DataChunk = Trim(DataChunk)
    FullData = FullData & DataChunk
    bytes.Clear(byt es, 0, CInt(tcpClient. ReceiveBufferSi ze))
    If DataChunk="EOF, AppConfirmation String"
    EOF = True
    End If
    End If
    Loop

    When a number of packets is received on the socket (e.g. client sending
    data line by line rather than a single stream), FullData always equals
    to whatever the first packet (line) included. It just doesn't seem to
    add DataChunk to itself each time loop is run. Why?
    When instead of this I try adding DataChunk to a textbox, it's fine, at
    the end I have all data sent to me in textbox...

    --
    Greets,
    Arkadiusz 'Winger' Slusarczyk
    Republic of Ireland
  • ShaneO

    #2
    Re: Merging data received via TCP

    Winger wrote:[color=blue]
    > Hi,
    >
    > I'm having a weird problem... Part of code first:
    >
    > Dim bytes(tcpClient .ReceiveBufferS ize) As Byte
    > Dim DataChunk as String
    > Dim FullData as String
    >
    > Do While EOF = False
    > If networkStream.D ataAvailable = True Then
    > networkStream.R ead(bytes, 0, CInt(tcpClient. ReceiveBufferSi ze))
    > DataChunk = Encoding.ASCII. GetString(bytes )
    > DataChunk = Trim(DataChunk)
    > FullData = FullData & DataChunk
    > bytes.Clear(byt es, 0, CInt(tcpClient. ReceiveBufferSi ze))
    > If DataChunk="EOF, AppConfirmation String"
    > EOF = True
    > End If
    > End If
    > Loop
    >
    > When a number of packets is received on the socket (e.g. client sending
    > data line by line rather than a single stream), FullData always equals
    > to whatever the first packet (line) included. It just doesn't seem to
    > add DataChunk to itself each time loop is run. Why?
    > When instead of this I try adding DataChunk to a textbox, it's fine, at
    > the end I have all data sent to me in textbox...
    >[/color]

    I am wondering if your code is contained within an Event?? If so, you
    may be looking at a re-entrant issue which could cause the problem
    you're describing. (Try temporarily defining "FullData" globally, that
    would expose this).

    Otherwise, try putting a Counter within your main Loop with output going
    to the Immediate Window, or using Debug.Print(Dat aChunk) after each
    "DataChunk = Trim(DataChunk) " line. This will show you what is being
    read into that Variable.

    ShaneO

    There are 10 kinds of people - Those who understand Binary and those who
    don't.

    Comment

    • Winger

      #3
      Re: Merging data received via TCP

      Hi,

      ShaneO wrote:
      [color=blue]
      > I am wondering if your code is contained within an Event?? If so, you
      > may be looking at a re-entrant issue which could cause the problem
      > you're describing. (Try temporarily defining "FullData" globally, that
      > would expose this).[/color]

      It's part of a function launched when there is connection pending on TCP
      port.
      [color=blue]
      > Otherwise, try putting a Counter within your main Loop with output going
      > to the Immediate Window, or using Debug.Print(Dat aChunk) after each
      > "DataChunk = Trim(DataChunk) " line. This will show you what is being
      > read into that Variable.[/color]

      I have debugged line that is supposed to append DataChunk to Fulldata a
      number of times and result was:
      - when line was about to be executed, it showed me these values:
      FullData = FullData & DataChunk
      "String1" "String1" "String 2"
      After the line was executed, FullData was still... "String 1". This is
      really weird for me.
      Working in VS .Net 2003 by the way.

      --
      Greets,
      Arkadiusz 'Winger' Slusarczyk
      Republic of Ireland

      Comment

      • ShaneO

        #4
        Re: Merging data received via TCP

        Winger wrote:[color=blue]
        > Hi,
        >
        > ShaneO wrote:
        >[color=green]
        >> I am wondering if your code is contained within an Event?? If so, you
        >> may be looking at a re-entrant issue which could cause the problem
        >> you're describing. (Try temporarily defining "FullData" globally,
        >> that would expose this).[/color]
        >
        >
        > It's part of a function launched when there is connection pending on TCP
        > port.
        >[color=green]
        >> Otherwise, try putting a Counter within your main Loop with output
        >> going to the Immediate Window, or using Debug.Print(Dat aChunk) after
        >> each "DataChunk = Trim(DataChunk) " line. This will show you what is
        >> being read into that Variable.[/color]
        >
        >
        > I have debugged line that is supposed to append DataChunk to Fulldata a
        > number of times and result was:
        > - when line was about to be executed, it showed me these values:
        > FullData = FullData & DataChunk
        > "String1" "String1" "String 2"
        > After the line was executed, FullData was still... "String 1". This is
        > really weird for me.
        > Working in VS .Net 2003 by the way.
        >[/color]
        All of the above would still indicate to me that you are indeed looking
        at a re-entrant issue. It would appear your "Function" is being
        simultaneously executed more than once.

        Did you try globally declaring "FullData"? You will need to remove (or
        REM) your "Dim FullData as String" line and declare it in the General
        Declarations section at the top of your program.

        I'd be interested to know the results.

        ShaneO

        There are 10 kinds of people - Those who understand Binary and those who
        don't.

        Comment

        • Winger

          #5
          Re: Merging data received via TCP

          ShaneO wrote:
          [color=blue]
          > All of the above would still indicate to me that you are indeed looking
          > at a re-entrant issue. It would appear your "Function" is being
          > simultaneously executed more than once.[/color]

          I use threading (ThreadPool) to launch new thread that handles incoming
          TCP connection. This way application can process data without blocking
          listening port and I'm ready for next connection...
          Yet, all tests were done with a single connection only, straight after
          program was started, so new thread was created only once...
          [color=blue]
          > Did you try globally declaring "FullData"? You will need to remove (or
          > REM) your "Dim FullData as String" line and declare it in the General
          > Declarations section at the top of your program.[/color]

          I've tried that, result was identical...

          I will be rewriting full code anyway (have few reasons for that), I will
          see wheter problem will still occur...

          --
          Greets,
          Arkadiusz 'Winger' Slusarczyk
          Republic of Ireland

          Comment

          Working...