jM2.me
jM2.me

Reputation: 3969

C# handle force closed socket connection

Let's say my client application looses connection with server application because of internet problems, the Server then receives a "An existing connection was forcibly closed by the remote host" exception.

What's the proper way to catch this exception, close the server-side socket and then remove it?

Upvotes: 2

Views: 1613

Answers (1)

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6891

this largely depends on what class you're using to establish and maintain the connection.

I typically use TcpClient, TcpListener and their asynchronous functions. When error occurs, the callback function is called with e.Error != null. What usually follows is the removal of the socket, which is conveniently available in e.UserState, from the list of active connections.

Upvotes: 1

Related Questions