Home All Groups Group Topic Archive Search About

Merging data received via TCP

Author
20 Apr 2006 1:42 AM
Winger
Hi,

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

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

Do While EOF = False
    If networkStream.DataAvailable = True Then
       networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
       DataChunk = Encoding.ASCII.GetString(bytes)
       DataChunk = Trim(DataChunk)
       FullData = FullData & DataChunk
       bytes.Clear(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
       If DataChunk="EOF,AppConfirmationString"
         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

Author
20 Apr 2006 2:44 AM
ShaneO
Winger wrote:
Show quoteHide quote
> Hi,
>
> I'm having a weird problem... Part of code first:
>
> Dim bytes(tcpClient.ReceiveBufferSize) As Byte
> Dim DataChunk as String
> Dim FullData as String
>
> Do While EOF = False
>    If networkStream.DataAvailable = True Then
>       networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
>       DataChunk = Encoding.ASCII.GetString(bytes)
>       DataChunk = Trim(DataChunk)
>       FullData = FullData & DataChunk
>       bytes.Clear(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
>       If DataChunk="EOF,AppConfirmationString"
>         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...
>

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(DataChunk) 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.
Author
20 Apr 2006 8:25 AM
Winger
Hi,

ShaneO wrote:

> 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).

It's part of a function launched when there is connection pending on TCP
port.

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

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
Author
20 Apr 2006 10:32 PM
ShaneO
Winger wrote:
Show quoteHide quote
> Hi,
>
> ShaneO wrote:
>
>> 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).
>
>
> It's part of a function launched when there is connection pending on TCP
> port.
>
>> Otherwise, try putting a Counter within your main Loop with output
>> going to the Immediate Window, or using Debug.Print(DataChunk) after
>> each "DataChunk = Trim(DataChunk)" line.  This will show you what is
>> being read into that Variable.
>
>
> 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.
>
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.
Author
21 Apr 2006 9:22 PM
Winger
ShaneO wrote:

> 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.

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...

> 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'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