Buffer overlap

K

Kenneth H. Young

I am getting over lapping data while sending and receiving data in a tcp listener I am working on. How does one reset or clear the buffer or bytes so they don't carry over to the next send or receive?

Thanks in advance!

Partial Example:

' Client sends:
TempS = curAdd.ToString & "....................................................................................................."

' Padding the end of the IP address so I can trim(".") when received because I couldn't trim the free space at the end.

Buffer = System.Text.Encoding.Default.GetBytes(TempS.ToString)

Client.GetStream().Write(Buffer, 0, Buffer.Length)



' The Server Receives:

While Not StopListener

If CurSocket.Available > 0 Then

' 1. Receive IP Address from client:

Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)

SyncLock CurThread

clientV = (System.Text.Encoding.Default.GetString(Buffer)).ToString

clientV = clientV.Trim(".")

Buffer.Clear(Buffer, Buffer.Length, 0)

On the next send / receive I am getting leftover ........... in the string.

THANKS!
 
M

Mattias Sjögren

Kenneth,

I'm not sure I understand the problem, but ...

Buffer = System.Text.Encoding.Default.GetBytes(TempS.ToString)

I'd recommend using an encoding other than Default (such as UTF-8),
because the default can be different on different systems.

Buffer.Clear(Buffer, Buffer.Length, 0)

Since you specify a length of xero here, nothing will be cleared.



Mattias
 
K

Kenneth H. Young

When I send this: TempS = curAdd.ToString &
"...................................................................................................."
to the server I then trim(".") to get rid of the periods. Then the server
sends four messages back to the client depending on the processes that takes
place. Finally the client Sends: "PCNAME-Scheduled" to the server and the
server receives "........ting". It appears as though its pieces of previous
buffer information.

I hope I explained the issue!

Thanks for the HELP!
 
P

Peter Huang [MSFT]

Hi

Firstly I think you may try the code in the link below for client/server.
TcpListener Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemnetsocketstcplistenerclasstopic.asp
TcpClient Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemNetSocketsTcpClientClassTopic.asp

If you still have any concern, can you build a simple reproduce sample and
send to me via removing "online" from my email address.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top