Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya
 asked on

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
Visual Basic.NET

Avatar of undefined
Last Comment
RadhaKrishnaKiJaya

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
mrwad99

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mike Tomlinson

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

RadhaKrishnaKiJaya

ASKER
Thank you very much. It worked.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy