Get the actual data length read from network stream

Hi Experts,

I am reading data from a RFID reader using network stream. But the length of the data is always 8193. What can I do to get the actual length.
Thanks in advance.

Dim tcpClient As New TcpClient("192.145.1.80", "10001")
             Dim netStream As NetworkStream = TcpClient.GetStream()
             If netStream.CanRead Then
                 ' Reads the NetworkStream into a byte buffer.
                 Dim bytes(TcpClient.ReceiveBufferSize) As Byte
                 ' Read can return anything from 0 to numBytesToRead.  
                 ' This method blocks until at least one byte is read.
                 netStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize))

                 ' Returns the data received from the host to the console.
                Dim ReturnData As String = Encoding.ASCII.GetString(bytes)
                 TextBox1.Text = ReturnData  'Length is always 8193
                RaiseEvent TCPDataIn(ReturnData)
             Else
                 Console.WriteLine("You cannot read data from this stream.")
                 TcpClient.Close()
                 ' Closing the tcpClient instance does not close the network stream.
                 netStream.Close()
                 Return
             End If
RadhaKrishnaKiJayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrwad99Commented:
NetworkStream.Read returns thenumber of bytes read, which you are not storing.

Dim numberOfBytesRead As Integer = 0
' ... as before
numberOfBytesRead = netStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize))

Open in new window


You are then free to use numberOfBytesRead in your output or to examine it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Adding to mrwad99's answer, use "numberOfBytesRead" in Encoding.ASCII.GetString():
Dim ReturnData As String = Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead)

Open in new window

0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much. It worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.