0

I am writing a program in VB.net that is connecting to a server, sending a byte array to the server and then receives a byte array as an answer then needs to receive a continuous stream of bits from the server.

The Sent Bit Array is 84 bytes and should be the same the first time it is received from the server and vary in size every time the server sends it back after that.

the start and the end of the message will always be the same but the size of the bit array sent will vary.

My issue is that I can write the bits to the server but I am not able to receive any of the follow-on bits that are broadcast.

Any suggestions

Public Sub connect(TcpClient As TcpClient)
    Dim NetworkStream As NetworkStream = TcpClient.GetStream()
    If networkStream.CanWrite And networkStream.CanRead Then
        ' Do a simple write.
        Dim sendBytes As [Byte]() = IO.File.ReadAllBytes(startupFile)
        networkStream.Write(sendBytes, 0, sendBytes.Length)

        ' Read the NetworkStream into a byte buffer.
        Dim bytes(1024) As Byte
        NetworkStream.Read(bytes, 0, bytes.Length)

        ' Output the data received from the host to the console.
        Dim returndata As String = Encoding.ASCII.GetString(bytes)

        Dim fileWrite As New IO.StreamWriter(writeFile)
        fileWrite.Write(returndata)
        fileWrite.Close()

    Else
        If Not networkStream.CanRead Then
            Console.WriteLine("cannot not write data to this stream")
            TcpClient.Close()
        Else
            If Not networkStream.CanWrite Then
                Console.WriteLine("cannot read data from this stream")
                TcpClient.Close()
            End If
        End If
    End If

End Sub
  • You should look into [**message framing**](http://stackoverflow.com/a/37352525/3740093). It helps you detect the start and end of data. – Visual Vincent Jan 31 '17 at 17:50
  • I am not able to modify the string before it is sent to me as it is on a piece of hardware I do not control. The byte array start is always a U32 as A1B2C3D4 and the end is U32 as 5E4D3C2B. – John Sheedy Feb 01 '17 at 18:12
  • Okay, but I'm not sure what you mean with _"is always a U32 **as A1B2C3D4**"_? And what's a _U32_? - `UInt32`? – Visual Vincent Feb 01 '17 at 18:16
  • Yes it is a UInt32. I was able to just set the buffer size to 80k and then read everything in the buffer then strip out the data between the Start and end bits. Thanks for the help. – John Sheedy Feb 17 '17 at 10:20

0 Answers0