C# Stream.ReadTimeout Property

…衆ロ難τιáo~ 提交于 2020-01-17 08:22:24

问题


I am looking into stream reader timeout property. From the documentation, I did not understand,

  • How this property works?
  • What will happen when stream reader timeouts?

Can somebody explain me these questions or point out to some better documentation than this.

https://msdn.microsoft.com/en-us/library/system.io.stream.readtimeout(v=vs.110).aspx

Sample code:

TcpClient client = new TcpClient(serverIP, serverTcpPort);
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
sr.BaseStream.ReadTimeout = 100;

回答1:


The documentation quite clearly says, that not every Stream implements ReadTimeout. Some subclasses of Stream may implement this property. So you need to check the documentation of subclasses to learn about the usage of ReadTimeout. Your code snippet would work with a NetworkStream, which is returned by

Stream s = client.GetStream();

The Microsoft website does have some specific documentation for this NetworkStream class and its ReadTimeout property, which you can find here: https://msdn.microsoft.com/en-us/library/bk6w7hs8(v=vs.110).aspx



来源:https://stackoverflow.com/questions/44693705/c-sharp-stream-readtimeout-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!