[英]Proper implementation of C# TCP reconnecting client
I have to write a TCP Client
that will have ability to reconnect to server. 我必须编写一个能够重新连接到服务器的
TCP Client
。 The server can be unavailable due to poor network connection quality or some maintenance issues. 由于网络连接质量差或某些维护问题,服务器可能无法使用。 I'm searching for quality solutions in this area.
我正在寻找这个领域的优质解决方案。
My current solutions is following: 我目前的解决方案如下:
TcpClient
class. TcpClient
类创建客户端。 ConnectionCheckTimer
, and ReconnectTimer
ConnectionCheckTimer
和ReconnectTimer
计时器 ConnectionCheckTimer
periodically checks lastMessageTimestamp and compares it with current time if the interval is greater then maxValue it launches ReconnectTimer
ConnectionCheckTimer
定期检查lastMessageTimestamp,并将其与当前时间进行比较,如果间隔大于maxValue则启动ReconnectTimer
Currently i'm not satisfied with this solution because it still generates exceptions for instance ObjectDisposedException on TcpClient.NetworkStream. 目前我对此解决方案不满意,因为它仍然在TcpClient.NetworkStream上生成例如ObjectDisposedException的异常。 I'm looking for some clean and reusable Tcp reconnecting client implementation that is able to cope with all sockets problems that can occur during connecting, disconnecting, reading data.
我正在寻找一些干净且可重复使用的Tcp重新连接客户端实现,它能够处理在连接,断开连接,读取数据期间可能发生的所有套接字问题。
If you have connection issues, you will always have exceptions. 如果您遇到连接问题,您将始终有例外。 I think you have a sound outline, you just need to handle the exceptions.
我认为你有一个完整的大纲,你只需要处理异常。 You could start with your own Socket class implemenation and write the TCPIP Server.
您可以从自己的Socket类实现开始并编写TCPIP服务器。 Starter code is at MS:
入门代码在MS:
http://msdn.microsoft.com/en-us/library/fx6588te(VS.71).aspx http://msdn.microsoft.com/en-us/library/fx6588te(VS.71).aspx
The C# code is half way down the VB page. C#代码是VB页面的一半。
The class you should use is "SocketAsyncEventArgs". 您应该使用的类是“SocketAsyncEventArgs”。 I've used it in this project:
我在这个项目中使用过它:
http://ts3querylib.codeplex.com/ http://ts3querylib.codeplex.com/
Have a look at the AsyncTcpDispatcher class. 看看AsyncTcpDispatcher类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.