Merging data received via TCP

W

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

ShaneO

Winger said:
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.
 
W

Winger

Hi,
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.
 
S

ShaneO

Winger said:
Hi,



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



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

Winger

ShaneO said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

NNTP Client 4
TCP Transfer issues 3
NetworkStream crisis!!! 1
How to debug this? 10
Data Logger 1
send file 1
Testing client server application from single computer 3
TCP/IP 3

Top