phxvyper
phxvyper

Reputation: 374

How to handle TcpClient disconnect from TcpListener

I have a client using TcpClient and a server using TcpListener. When the client disconnects from the server, how do i handle this? And what if the program crashes, how do i handle the disconnect then?

Upvotes: 2

Views: 1599

Answers (1)

Paul Farry
Paul Farry

Reputation: 4768

If your program crashes, all current Connections in the operating system will be closed.

With the TCPClient you can assign that to your own class (in a constructor etc) and have an Event come out of your class that is for Disconnects.

Then in your listener when a connection arrives, create your class add a reference to the class somewhere(List, Dictionary etc). Attach the event and proceed happily.

If you want to handle a servered Connection Disconnect at the socket level it usually is only when you want to "Write" to the socket that you can actually detect that it's no longer connected on the remote end unless you have graceful disconnect sent properly.

Upvotes: 1

Related Questions