Click here to Skip to main content
15,913,213 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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;


I have a ReceiveFrame function similar to this:
C#
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);
    }
}


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.
Posted
Updated 12-Dec-10 2:45am
v2
Comments
Abdul Quader Mamun 12-Dec-10 8:46am    
Spelling check.

1 solution

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.
 
Share this answer
 
v4
Comments
Espen Harlinn 26-Feb-11 10:50am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 21:48pm    
Thank you very much.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900