1

I have a multi-threaded C# program where each thread uses TcpClient to synchronously receive text/line oriented data from multiple servers.

It has run without errors for 2 years, but when the vendor providing the server changed, we started seeing issues where the vendor server program had a buffer overrun, and their fix (apparently) prevented them from core-dumping, but also they stop sending to us. But they do not tear down the TCP connection, so both sides show it as ESTABLISHED.

We were told by the vendor that another customer worked around this by simply implementing a timeout and re-establishing the connection if no data was received after a configurable timeout.

Since I implemented my threads using a synchronous model (essentially, a 'while readline()'), can I use the TcpClient ReceiveTimeout property and expect it to work properly? I tried it with a simpler non-threaded version, and it seemed to work the first loop, but not afterwards.

I really do not want to have to rewrite the program to use an asynchronous socket model.

Any suggestions about a simple way to timeout using synchronous TcpClient would be greatly appreciated.

Mitch

Addendum: when I received the exception on the ReadLine(), I do close the TcpClient and start over, just as if I received a network disconnect. By "not working", I mean that it stopped timing out after a few times, even though 1.) I was still not being sent anything and 2.) I was sitting blocked in a ReadLine().

1
  • Please elaborate on "it seemed to work the first loop, but not afterwards". Note that once you have a failure on a socket (including a timeout), you have to close it and start over. Oct 20, 2014 at 17:34

1 Answer 1

1

ReceiveTimeout only works with synchronous IO. There is no timeout for async socket reads. If ReceiveTimeout did not work for you there is some bug that is not apparent from the info given in the question. In that case post repro code.

2
  • Ok, I will be re-testing tomorrow... I will get back if things are still not working...
    – mitchmcc
    Oct 20, 2014 at 18:35
  • FYI, it looks like it is working now... not sure why I thought it did not before (?). Thanks for the help.
    – mitchmcc
    Oct 21, 2014 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.