0

I was wondering if there is any method or property that allow us to see if there are available bytes to read in the stream associated to a BinaryReader (in my case, it is a NetworkStream, since I am performing a TCP communication). I have checked the documentation and the only method I have seen is PeekChar(), but it only checks if there is a next byte (character), so in case there are many bytes to read, making a while loop to increase a counter may be inneficient.

Regarding the TCP communication, the problem is that the application protocol behind the TCP was not defined by me, and I am just trying to figure out how it works! Of course there will be some "length field" that will give me some clues about the bytes to read, but right know I am just checking how it works and this question came to my mind.

7
  • TCP is a streaming protocol... not sure what you mean, but length is pretty much the opposite of the definition of streaming. BinaryReader is just a wrapper around the protocol stream. Commented May 27, 2016 at 7:17
  • Mmm... I am sorry but I don't understand your point! Commented May 27, 2016 at 7:18
  • 3
    What do you plan to do if there aren't any bytes available right now? That doesn't mean that there isn't more coming... Commented May 27, 2016 at 7:18
  • The specific problem here with a TCP stream is that, as soon as you have determined that the answer is "no", or that there are 20 bytes available, those answers may no longer be accurate. Commented May 27, 2016 at 7:19
  • 3
    That's a different problem: use a non-blocking (or async) read. Commented May 27, 2016 at 7:22

2 Answers 2

1

The BinaryReader itself doesn't have a DataAvailable property, but the NetworkStream does.

NetworkStream stream = new NetworkStream(socket);
BinaryReader reader = new BinaryReader(stream);

while (true)
{
    if (stream.DataAvailable)
    {
        // call reader.ReadBytes(...) like you normally would!
    }
    // do other stuff here!
    Thread.Sleep(100);
}

If you don't have a handle to the original NetworkStream at the point where you would call reader.ReadBytes(), you can use the BaseStream property.

while (true)
{
    NetworkStream stream = reader.BaseStream as NetworkStream;
    if (stream.DataAvailable)
    {
        // call reader.ReadBytes(...) like you normally would!
    }
    // do other stuff here!
    Thread.Sleep(100);
}
Sign up to request clarification or add additional context in comments.

Comments

0

BinaryReader will block until it reads all bytes required. The only exception is if it detects end of stream. But NetworkStream is an open stream and does not have the end of stream condition. So you can either create class with basic readers (ReadInt, ReadDouble, etc) that uses peek, reads byte by byte and does not block; or use another async technology.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.