本文介绍了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.

推荐答案


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

10-21 13:07