Socket: Cancel / Timeout StreamReader.ReadToEnd()

M

Manfred Denzer

Hello!

I have a Sockt-Connection:
TcpClient socket = new TcpClient();
socket.Connect(this.address, this.port);
Stream inOut = socket.GetStream();
StreamReader sr = new StreamReader(inOut);

I use "ReadToEnd" to read from the Stream:
String receive = sr.ReadToEnd();

But if the remote station doesn't write something in the stream,
"ReadToEnd" is waiting infinitely. How can I cancel the StreamReader
after some seconds/milliseconds? Or can I set a Timeout that the
StreamReader cancel automatically?

Thank you very much for your help!
Manfred Denzer
 
M

Manfred Denzer

Yeah, I solved the Problem with a second thread!
In the second thread, I wait some seconds and close the StreamReader.
If "ReadToEnd" works, I abort the Thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top