问题
I'm developing some software that listens for events from another program via TCP.
The events come in a string formatted like this: "value1;value2;value3" The problem is that sometimes a single character from value1 gets read onto the previous event so i get something like this:
value1;value2;value3v
alue1;value2;value3
How can i make it know where each message begins and ends?
Here is the code:
Dim client As New TcpClient()
Sub listen()
Dim networkStream As Stream = client.GetStream()
While True
Dim bytes(client.ReceiveBufferSize) As Byte
Dim size As Integer = 0
size = networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
'process event here
End While
End Sub
This is all done on a thread of it's own.
回答1:
Sender has to included the length of each data its sending so at receiver you will receive upto the provided length. Easier option would be to include NULL termination so that you can read it as individual string at receiver end.
来源:https://stackoverflow.com/questions/23868913/stream-from-tcpclient-catching-data-from-next-packet