Real Master
Real Master

Reputation: 347

If no data is received over stream tcpclient close the connection

I would like to know on TcpClient's NetworkStream what exactly happen if timeout occurs.

While debugging the code i found that after request is sent and if no data is received within mention timeout period it throws below exception and unfortunately closes the connection (TcpClient.Connected become false):

Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

It throws the exception is okay, but i would like to know how i can prevent it from closing the connection.

It would be great if someone can provide more insights on this.

Upvotes: 0

Views: 640

Answers (1)

Carlos Torrecillas
Carlos Torrecillas

Reputation: 5756

Have you checked this one? Reconnect TCPClient after interruption I think if you have a long enough TTL of your TCP Connection, should an exception occurs (I believe you would get thrown a SocketException) you can catch that up and initiate your retry logic. There are several implementations for this and obviously that would depend on the use case but normally there is a number of attempts (configuration value) before "giving up" connecting. That way your manager will retry connecting X number of times and will carry on if there is a successful connection otherwise will propagate up in the chain the exception.

Upvotes: 1

Related Questions