Вопрос

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.

Это было полезно?

Решение

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...)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top