NetworkStream crisis!!!

C

carlos_rocha

Hi!
I'm having a problem that is driving me crazy!I made a tcp server and
client and when i exchange data between them, at the client the
stream seems to overlap the following data to the previous ones.For
example, if i write to the client "Hello client" the first time, and
then write "hi" the reader gets "hillo client".When i use the server
with telnet, the telnet shell receives the data correctly so i think
the problem may be in the client(?).I cannot solve this problem, Can
anyone help me pleaaaaaaase.I bet it's something stupid...

Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Server returned: " + returndata))
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
returndata = Encoding.ASCII.GetString(bytes)
Console.Write(returndata)

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
D

David Browne

carlos_rocha said:
Hi!
I'm having a problem that is driving me crazy!I made a tcp server and
client and when i exchange data between them, at the client the
stream seems to overlap the following data to the previous ones.For
example, if i write to the client "Hello client" the first time, and
then write "hi" the reader gets "hillo client".

You need to keep track of how many bytes you read with each Read.
NetworkStream.Read returns an Integer indicating the number of bytes read.
Your second read returned two bytes, so you should only have decoded the
first two bytes in the buffer.

Further, you need some protocol so your client and server know when they
have received the whole message. Telnet uses Crlf, HTTP uses
content-length, etc.

David
 

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
How to debug this? 10
TCP Transfer issues 3
send file 1
Merging data received via TCP 4
networkstream.read 1
TCP/IP 3
Data Logger 1

Top