TCP 服务器截断从网络流读取的消息

问题描述 投票:0回答:1

我已经实现了这样的 TCP 侦听器:

Using client As TcpClient = _server.AcceptTcpClient()

                Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)

                Using ns As NetworkStream = client.GetStream()

                    While ns.DataAvailable

                        Dim toReceive(100000) As Byte

                        Dim length As Integer = ns.Read(toReceive, 0, toReceive.Length)

                        If length > My.Settings.minLenghtForSuccess Then
                            Dim text As String = Encoding.UTF8.GetString(toReceive, 0, length)
                            text = "{" & text.Split("{")(1)
                            Dim jsonObj As Object = Newtonsoft.Json.JsonConvert.DeserializeObject(text)
                            Dim ipOrigine As String = ""
                            Dim dispositivoOrigine As String = ""
                            Dim canaleOrigine As String = ""
                            Dim targa As String = ""
                            Dim frontespizio = jsonObj("data")(0)
                            ipOrigine = frontespizio("IPAddress")
                            dispositivoOrigine = frontespizio("DeviceName")
                            canaleOrigine = frontespizio("ChannelName")
                            targa = frontespizio("PicData")(0)("PlateInfo")(0)("SnapId")

                        End If
                        'Console.WriteLine(text)
                        'Console.WriteLine()

                        'Dim toSend() As Byte = Encoding.ASCII.GetBytes("Message Received...")

                        'ns.Write(toSend, 0, toSend.Length)

                    End While

                End Using

            End Using

当它收到来自客户端的消息时,它会被截断,我不知道为什么。我认为这是为了 toReceive(100000) var。我该如何解决这个问题? 我收到的消息类似于:

POST /API/AlarmEvent/EventPush

HTTP/1.1 主机:192.168.2.103:123

接受:/

内容类型:application/json;字符集=UTF-8

内容长度:106438

{"结果":"成功","数据":{"EventType":"LPD","EventTime":"2015-1-15 9:53:39","EventAction":"开始","ChannelName":"PERGENOVA","DeviceName":"BS-IPT4704G3MZ+","IPAddress":"192.168.2.168","MacAddress":"00- 23-63-92-25-47","PicData":{"PlateInfo":[{"Id":"","GrpId":3,"SnapId":"AH234MG","类型":10," StrChn":"CH1","开始时间":1421315612,"结束时间":1421315656,"Chn":0,"性别":0,"车牌颜色":0,"汽车品牌":"","汽车类型":"" ,"所有者":"","IdCode":"","工作":"","电话":"","住所":"","备注":"","PlateImg":"/9j /4AAQSkZJRgABAQIAdgB2AAD/7wAPAAAAAAAAAAAAAAAAAP/bAEMAAwICAwICAwMDAwQDAwQFCAUFBAQFCgcHBggMCgwMCwoLCw0OEhANDhEOCwsQFHARExQVFRUMDxcYFhQYEhQVFP/bAEMBAwQEBQQFCQUFCRQNCw0UFB="}]}}}

  • PlateImg 非常长,超过 100000 字节且长度不固定
json vb.net buffer tcplistener
1个回答
1
投票

这可能是由于

toReceive
缓冲区的大小是固定的。您将
toReceive
初始化为固定大小 100,000 字节,如果传入消息大于该大小,它将如您所观察到的那样被截断。

尝试使用动态缓冲区。

例如

Using client As TcpClient = _server.AcceptTcpClient()
    Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)

    Using ns As NetworkStream = client.GetStream()
        Dim buffer As New MemoryStream() ' Use MemoryStream as a dynamic buffer

        Dim bytesRead As Integer
        Dim receiveBuffer(4096) As Byte ' Buffer to read incoming data in chunks

        Do
            bytesRead = ns.Read(receiveBuffer, 0, receiveBuffer.Length)
            If bytesRead > 0 Then
                buffer.Write(receiveBuffer, 0, bytesRead)
            End If
        Loop While bytesRead > 0

        buffer.Seek(0, SeekOrigin.Begin) ' Reset the position of the MemoryStream

        Dim receivedData(buffer.Length) As Byte
        buffer.Read(receivedData, 0, receivedData.Length)

        ' Now you have the complete message in the receivedData array as bytes.

        Dim text As String = Encoding.UTF8.GetString(receivedData)
        ' Rest of your code to process the message...

    End Using
End Using

使用

MemoryStream
作为动态缓冲区,您可以分块读取传入数据(在本例中为4096字节)并将其附加到缓冲区,直到没有更多数据可供读取。然后,您可以将
MemoryStream
内容转换为字节数组并继续处理收到的消息。

© www.soinside.com 2019 - 2024. All rights reserved.