我必须写一份<代码>TCP客户,以便能够与服务器重新连接。 由于网络连接质量差或一些维修问题,服务器无法使用。 寻找该领域的质量解决办法。
我目前的解决方案如下:
- keep connection state in ConnectionState enum {Offline, Online, Connecting}
- create client with
TcpClient
class. - create two timers called
ConnectionCheckTimer
, andReconnectTimer
- connect to server
- start reader thread and connection check timer
- reading is performed with tcpClient.GetStream() and then reading from this stream
- when Exception is caught in readerLoop client state is changed to offline and ReconnectTimer is launched
ConnectionCheckTimer
periodically checks lastMessageTimestamp and compares it with current time if the interval is greater then maxValue it launchesReconnectTimer
目前,由于这种解决办法仍然产生例外,例如对TcpClient的反对意见。 网络Stream。 I m 寻找一些清洁和可再利用的Tcp重新连接客户执行工作,以便能够应对连接、脱节、阅读数据过程中可能出现的所有袖珍问题。