【问题标题】:Safely timeout ReadLine on a network stream在网络流上安全地超时 ReadLine
【发布时间】:2012-05-18 18:31:28
【问题描述】:

如何安全地使以下代码超时?

var client = new TcpClient("localhost", 6000);
new StreamReader(client.GetStream()).ReadLine(); // close the client if no line arrives after 5 seconds...

以前我在另一个线程上运行了一个计时器,如果超时过期,它会调用 client.Close(),但我不认为像这样调用 client.Close() 是线程安全的?

【问题讨论】:

  • 为什么不使用 TcpClient.ReceiveTimeout?

标签: c# timeout tcpclient


【解决方案1】:

您可以使用内置的ReceiveTimeout 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2020-04-06
    • 2015-03-19
    相关资源
    最近更新 更多