Correct C # TCP Reconnect Client Implementation

I need to write TCP Clientthat will be able to reconnect to the server. The server may not be available due to poor network connectivity or some maintenance issues. I am looking for quality solutions in this area.

My current solutions are as follows:

  • save connection state in ConnectionState enum {Offline, Online, Connection}
  • create a client with a class TcpClient.
  • create two timers called ConnectionCheckTimerandReconnectTimer
  • connect to server
  • start reading and connection check timer
  • reading is done using tcpClient.GetStream () and then reading from this stream
  • When the exception enters the reader reading state, the client state changes offline and ReconnectTimer starts
  • ConnectionCheckTimer periodically checks lastMessageTimestamp and compares it with the current time, if the interval is greater than maxValue, it starts ReconnectTimer

I am currently not satisfied with this solution because it still throws exceptions, such as ObjectDisposedException, in TcpClient.NetworkStream. I am looking for some clean and reusable client implementation of Tcp reconnection, which can deal with all socket problems that may occur when connecting, disconnecting, reading data.

+3
source share
2 answers
+1

, , "SocketAsyncEventArgs". :

http://ts3querylib.codeplex.com/

AsyncTcpDispatcher.

0

Source: https://habr.com/ru/post/1758936/


All Articles