NetworkStream.ReadTimeout属性出现问题 [英] Problem with NetworkStream.ReadTimeout property

查看:705
本文介绍了NetworkStream.ReadTimeout属性出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该类的ReadTimeout属性似乎只能工作一次.我创建对象如下:

The ReadTimeout property of that class seems to work only once. I create the objects as follows:

this.mySocket = new TcpClient();
this.mySocket.Connect(this.ipAddress,this.tcpPort);
this.stream = this.mySocket.GetStream();
this.stream.ReadTimeout = this.Timeout;



我有一个类似于以下的ReceiveFrame函数:



I have a ReceiveFrame function similar to this:

public override void ReceiveFrame(byte[] buf,out int longitud)
{
    longitud = 0;
    try
    {
        longitud = this.stream.Read(buf, longitud, buf.Length);
    }    
    catch (SocketException)
    {
    }
    catch (IOException)
    {
        //Debug.Assert(this.mySocket.Connected);
    }
}



它会收到罚款.当帧未按时到达时,超时将按预期发生.在这种情况下,如果有新数据到达,则可能再次尝试发送和接收,但是如果没有,则预期的超时不会再发生.

很好奇的是,当第一次超时发生时,它说socket.Connected为false(尽管仍然处于连接状态,如果您尝试再次连接,则会出现错误).

有人知道如何使ReadTimeout继续工作吗?谢谢,

Ruben.



It receives fine. When a frame doesn''t arrive on time the timeout occurs as expected. After that situation making another attempt to transmit and receive may work if new data arrive, but the expected timeout never occurs again in case they don''t.

It''s curious that when the first timeout occurs it says that socket.Connected is false (although still connected, giving an error if you try to connect again).

Does anybody know how to make ReadTimeout keep on working? Thanks,

Ruben.

推荐答案

不要通过提供空的异常处理程序来关闭异常传播(在实践中,这样做的频率比以前少,但是从不:);我希望这是您在现实生活中真正要做的事情.

我认为,一旦指定ReadTimeout,它便会一直有效.但!您可能忽略了甚至在进入线程的等待状态之前都抛出SocketExceptionIOException的情况.这是迄今为止您所观察到的这类问题的最典型原因. 提供这两个异常的完整转储(不要做您注释掉的断言),您将看到结果,对其进行分析.

如果您发现了异常之一,但未能解决问题,请在此处提交转储的文本,以便我查看.

为了提供良好的异常转储,您可以在其他答案中使用我建议的代码,例如,此处.

谢谢.
Do not shut up exception propagation by providing empty exception handler (in practice, do it less often then never:); I hope this is what you really do in real life).

I think once you specify ReadTimeout, it always work. But! chances are you have overlooked the situation when either SocketException or IOException was thrown even before entering your thread''s wait state. This is by far the most typical reason for the problems of a kind you observe.
Provide a full dump of those two exceptions (do not do this assertion you commented out) and you will see the result, analyze it.

If you have caught one of the exception but fail to figure out the problem, please submit the text of your dump here so I could see it.

To provide good exception dump, you could use, for example, my code I advice in different answer, here.

Thank you.


这篇关于NetworkStream.ReadTimeout属性出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆