Tcpclient資料傳輸的問題

Andasan
各位大大
我利用TCPClient與伺服端連線後,從伺服端傳送資料過來,如果TCPClient是寫在表單,那沒有問題,但是如果是寫在類別,同樣的程式碼,傳輸過來後,上一次的傳輸並不會不見,只會被取代掉。
例如:第一次傳abcdefg,那接收到的是abcdefg,這沒問題。可是第二次傳123,接收到的卻是123defg。寫在類別中就會有這種情況出現,請各位大大指點。



 Public Sub ReceiveData(ByVal clientConnect As TcpClient)
        Dim mystring as String
        Dim myNetworkStream As NetworkStream = clientConnect.GetStream() '接收資料
        Dim InBytesCount As Integer = 0
        Dim myReceiveBytes(1024) As Byte
        Dim i As Integer = 0
        While True
            Try
                InBytesCount = myNetworkStream.Read(myReceiveBytes, 0, myReceiveBytes.Length)
                System.Threading.Thread.Sleep(100)
                If InBytesCount = 0 Then
                    Exit While
                End If
                mystring = Encoding.GetEncoding(950).GetString(myReceiveBytes).TrimEnd().TrimStart()
                Msgbox(mystring)
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
                Exit Sub
            End Try
        End While


    End Sub

Andasan
後來我反覆測試,就是要清除掉myReceiveBytes的資料就可以了,但我還是搞不清除為甚麼在表單內使用就不必。
.
.
Try 
Array.Clear(myReceiveBytes, 0, myReceiveBytes.Length)
.
.
End Try

回到頂部