Socket: Cancel / Timeout StreamReader.ReadToEnd()

  • Thread starter Thread starter Manfred Denzer
  • Start date Start date
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
 
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.
 
Back
Top