Visual Basic TCP数据损坏

时间:2018-05-09 02:05:25

标签: vb.net tcp corruption networkstream

我有一个使用TCP进行客户端和服务器之间通信的程序。客户端发送BMP图像的字节数组。侦听器以块的形式读取字节并将字节连接在一起以获取图像。当服务器/监听器收到图像时,它几乎已经损坏了一半。

当使用localhost在我的计算机上进行测试时,不太可能发生损坏(字节数组大小越大=损坏越多),但是当通过互联网时,无论大小如何,它都会在99%的时间内损坏。我也尝试拆分字节数组并将其以块的形式发送到服务器,这似乎解决了localhost上的损坏,但在互联网上仍然是相同的。

可以告诉我这里我做错了吗?

在计时器中运行的侦听器代码:

If Listener.Pending = True Then
Client = Listener.AcceptTcpClient()
Message = ""
Dim ClientNetworkStream As NetworkStream = Client.GetStream
ReDim ReadBytes(-1)
If ClientNetworkStream.CanRead = True Then
    Dim ReadBuffer(65535) As Byte
    Dim NumberOfBytesRead As Integer = 0
    Do
    NumberOfBytesRead = ClientNetworkStream.Read(ReadBuffer, 0, ReadBuffer.Length)
    If Not NumberOfBytesRead = 0 Then
        If Not NumberOfBytesRead = -1 Then
            ReadBytes = AppendByteArrays(ReadBytes, ReadBuffer, NumberOfBytesRead)
            If ConvoOngoing = False Then
            ReDim CurrentConvoBytes(-1)
            ConvoOngoing = True
            CurrentConvoBytes = ReadBytes
        Else
            CurrentConvoBytes = AppendByteArrays(CurrentConvoBytes, ReadBytes)
        End If
    End If
End If
Loop While ClientNetworkStream.DataAvailable
    If ClientNetworkStream.DataAvailable = False Then
        If ConvoOngoing = True Then
            If ReadBytes.Length > 0 Then
                CurrentConvoBytes = AppendByteArrays(CurrentConvoBytes, ReadBytes)
            End If
        ConvoOngoing = False
        PictureBox1.Image = ConvertByteArrayToBMP(CurrentConvoBytes)
        End If
    End If
End If
End If

以下是发件人代码:

    Private Sub SendWhole(ByVal BytesToSend() As Byte, ByVal Optional isFile As Boolean = False)
    Client = New TcpClient(TextBox2.Text, TextBox3.Text)
    Dim ClientStream As NetworkStream = Client.GetStream
    ClientStream.Write(BytesToSend, 0, BytesToSend.Length)
    ClientStream.Close()
End Sub

代码中使用的AppendByteArrays代码:

    Public Function AppendByteArrays(ByVal OriginalArray As Byte(), ByVal ArrayToAppend As Byte(), ByVal Optional AmountOfValuesToAdd As Integer = -1) As Byte()
    Dim IndexToStart As Integer = OriginalArray.Length
    Dim Modifier As Integer = 0
    If Not AmountOfValuesToAdd = -1 Then
        Modifier = AmountOfValuesToAdd - 1
    Else
        Modifier = -1
    End If
    If Modifier = -1 Then
        ReDim Preserve OriginalArray((OriginalArray.Length) + (ArrayToAppend.Length - 1))
        For i = 0 To ArrayToAppend.Length - 1
            OriginalArray(IndexToStart + i) = ArrayToAppend(i)
        Next
    Else
        ReDim Preserve OriginalArray((OriginalArray.Length) + Modifier)
        For i = 0 To Modifier
            OriginalArray(IndexToStart + i) = ArrayToAppend(i)
        Next
    End If
    Return OriginalArray
End Function

编辑:这里有一点我似乎无法弄清楚数学正确

        If Listener.Pending = True Then
        Client = Listener.AcceptTcpClient()
        Dim ClientNetworkStream As NetworkStream = Client.GetStream
        ReDim ReadBytes(-1)
        If ClientNetworkStream.CanRead = True Then
            Dim ReadBuffer(65535) As Byte
            Dim NumberOfBytesRead As Integer = 0
            Do
                NumberOfBytesRead = ClientNetworkStream.Read(ReadBuffer, 0, ReadBuffer.Length)
                If Not NumberOfBytesRead = 0 Then
                    If Not NumberOfBytesRead = -1 Then
                        If ReadingPacket = False Then
                            If PacketHasLeftOvers = True Then
                                PacketHasLeftOvers = False
                                ReadBuffer = AppendByteArrays(PacketLeftOvers, ReadBuffer)
                                ReDim PacketLeftOvers(-1)
                            End If
                            ReadingPacket = True
                            PacketToReadSize = BitConverter.ToInt32(ReadBuffer, 0)
                            PacketType = ReadBuffer(4)
                            ReDim CurrentConvoBytes(-1)
                            ReadBytes = AppendByteArrays(ReadBytes, ReadBuffer.Skip(5).ToArray, NumberOfBytesRead - 5)
                            CurrentConvoBytes = ReadBytes
                            PacketReadSize = ReadBytes.Length - 1
                            If PacketReadSize = PacketToReadSize Then
                                'Check if already finished reading
                                PictureBox1.Image = ConvertByteArrayToBMP(CurrentConvoBytes)
                                PacketReadSize = 0
                                ReadingPacket = False
                            End If
                        Else
                            Dim PacketScore As Integer = PacketReadSize + NumberOfBytesRead
                            If PacketScore < PacketToReadSize Then
                                ReadBytes = AppendByteArrays(ReadBytes, ReadBuffer, NumberOfBytesRead)
                                CurrentConvoBytes = AppendByteArrays(CurrentConvoBytes, ReadBytes)
                                PacketReadSize += ReadBytes.Length - 1
                            ElseIf PacketScore > PacketToReadSize Then
                                ReadBytes = AppendByteArrays(ReadBytes, ReadBuffer, NumberOfBytesRead - (PacketToReadSize - PacketReadSize))
                                CurrentConvoBytes = AppendByteArrays(CurrentConvoBytes, ReadBytes)
                                PacketHasLeftOvers = True
                                PacketLeftOvers = ReadBuffer.Skip(PacketToReadSize - PacketReadSize).ToArray
                                PictureBox1.Image = ConvertByteArrayToBMP(CurrentConvoBytes)
                                PacketReadSize = 0
                                ReadingPacket = False
                            ElseIf PacketScore = PacketToReadSize Then
                                ReadBytes = AppendByteArrays(ReadBytes, ReadBuffer, NumberOfBytesRead)
                                CurrentConvoBytes = AppendByteArrays(CurrentConvoBytes, ReadBytes)
                                PictureBox1.Image = ConvertByteArrayToBMP(CurrentConvoBytes)
                                PacketReadSize = 0
                                ReadingPacket = False
                            End If
                        End If
                    End If
                Else
                    Throw New Exception
                End If
            Loop While ClientNetworkStream.DataAvailable
        End If
    End If

0 个答案:

没有答案