Stream from TCPClient catching data from next packet

ⅰ亾dé卋堺 提交于 2019-12-12 01:47:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!