中断后重新连接的TcpClient [英] Reconnect TCPClient after interruption

查看:1211
本文介绍了中断后重新连接的TcpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端应用程序的多个实例,连接到主应用程序通过互联网由TcpClient的。 (由我两个codeD)。因此,连接是这样的:

TcpClient.Connect(IP,端口)

我现在想这个处理各种类型的断开事件:

  1. 主要的应用程序(服务器)或客户端应用程序的计算机丢失互联网连接。

    • 在连接恢复,通讯似乎失去,但是当我尝试重新连接,我得到的消息: 的连接请求在已连接的套接字
    • 所以,我需要关闭并重新启动客户端应用程序。
  2. 主要的应用程序(服务器)被关闭,并重新启动。

    • 重新启动主应用程序,然后尝试重新连接客户端的应用程序,如上述相同的错误的结果。

那么,什么才是我需要做什么?我是否需要实例化一个新的TcpClient的客户端应用程序,只要这种中断发生的?我还没有尝试过,所以不知道这是一个贫穷的解决方案?

解决方案
  

我需要实例化一个新的TcpClient的客户端应用程序,只要这种中断发生的?

是的。如果再由TcpClient的psented $ P $连接断开,则不能使用作进一步沟通的对象,也不能重新连接。创建一个新的TcpClient对象。

您的问题很可能是在NAT网关超时的TCP连接,所以没有什么可以通过您的服务器及之间的LT; - >客户端,如果你所有的客户端不会从连接读,也不会发现这种情况下,它认为该连接仍然是开放的。

I have multiple instances of a client application, connecting to a main application over internet by TcpClient. (Both coded by me). So the connection is made like:

TcpClient.Connect(ip, port)

I now want this to handle various type of disconnect events:

  1. Main App (server) or a client app computer lose internet connection.

    • On resume of connection, the communication seems lost, but when I try to reconnect, I get message: "A connection request was made on an already connected socket"
    • So I need to close and restart the client app.
  2. Main App (server) is closed, and restarted.

    • Restarting the Main App, and then trying to reconnect the client app, results in same error as above.

So, what do I need to do? Do I need to instantiate a New TcpClient in the Client Apps, whenever such interruption occur? I haven't tried that, so don't know if that's a poor solution?

解决方案

do I need to instantiate a New TcpClient in the Client Apps, whenever such interruption occur?

Yes. If the connection represented by the TcpClient is broken, you can't use that object for further communicating, nor can you connect it again. Create a new TcpClient object.

Your problem is likely that a NAT gateway times out your TCP connection, so nothing can get through between your server<->client, if all your client does is read from the connection, it will not discover this case, and it thinks the connection is still open.

这篇关于中断后重新连接的TcpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆