0

I have an internet stream and I am creating reader from that

var reader = new BinaryReader(stream);

and after that I am reading bytes for searching "sync word"

if (reader.ReadByte() != magic[i++])

But sometimes i have a problem with ReadByte(). I expected that if reader cannot read bytes from the stream it will throw exception, but it not throwing anything. (Server is not shutdown, just don't provide bytes).

So my question is - how to time out this method? I can use TPL with CancellationToken, but don't understand how to cancel method ReadByte when it is needed.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Roman Golenok
  • 1,427
  • 9
  • 26

1 Answers1

3

Given that we now know this is from a TcpClient, I'd suggest setting the TcpClient.ReceiveTimeout before requesting the stream. That should make the read call time out appropriately.

(On the other hand, I didn't think TcpClient was available on WP7, so I'm not sure how you're using it...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I using SocketEx library for that. Thx, I will see to change to native way client-server communication via standard WP7 sockets. – Roman Golenok Jan 17 '13 at 11:57