网络流返回的字符串长度始终为8193

时间:2013-01-22 17:32:55

标签: .net vb.net string networkstream trim

我正在网络上发送一个字符串,每当我检查接收字符串的一方时,它总是告诉我收到的字符串长度为8193。但是当我尝试在MsgBox中显示之前分配一个值时,它会返回正确的值。我尝试使用String.Trim(Chr(10)) *,String.Trim(Chr(13))String.Trim(ChrW(8193))修剪字符串,但无济于事。

有人对此有任何想法吗?

这是接收方的代码。

Private Sub ChatConnectionTimer_Tick(sender As Object, e As System.EventArgs) Handles ChatConnectionTimer.Tick

    Try
        Dim ConnectionStatus As String = String.Empty

        If CBool(chatClientSocket.Available) Then
            chatNetworkStream = chatClientSocket.GetStream()
            Dim ByteData(chatClientSocket.ReceiveBufferSize) As Byte
            chatNetworkStream.Read(ByteData, 0, CInt(chatClientSocket.ReceiveBufferSize))
            ConnectionStatus = Encoding.ASCII.GetString(ByteData)

            MsgBox(ConnectionStatus.Length()) 'This part always return 8193

            If ConnectionStatus.Contains("Server is disconnected.") Then
                DisconnectChat()
            End If
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub

1 个答案:

答案 0 :(得分:2)

您的ByteData缓冲区始终是ReceiveBufferSize的大小。你想要做的是检查多少.Read返回并将其用作返回消息的长度。