Reconnecting TCPClient After Interruption

I have several instances of a client application connecting to the main application via the Internet TcpClient. (Both are encoded by me). Thus, the connection is performed as follows:

TcpClient.Connect (ip, port)

Now I want this to handle various types of disconnect events:

  • The main application (server) or the computer of the client application lose their Internet connection.

    • When reconnecting, the message seems to be lost, but when I try to connect, I get the message: "The connection request was made on an already connected socket
    • Therefore, I need to close and restart the client application.
  • The main application (server) is closed and restarts.

    • Restarting the main application and then trying to reconnect the client application results in the same error as above.

So what do I need to do? Do I need to instantiate a new TcpClient in client applications when such an interrupt occurs? I have not tried this, so I don’t know, is this a bad decision?

+3
source share
2 answers

Do I need to instantiate a new TcpClient in client applications when such an interrupt occurs?

Yes. If the connection provided by TcpClient is broken, you cannot use this object for further communication, and you cannot connect it again. Create a new TcpClient object.

, , NAT TCP-, , , , , .

+6

, . , , , , .. , . /p >

Socket, . , . , . - , , "" . .

, , . - .

+1

All Articles