Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I tell When a Socket has Disconnected

Tags:

c#

.net

sockets

I am using an ElSSLSocket from C#. This is a SecureBlackBox socket. SecureBlackBox is a third party library to handle secure sockets.

I can see that the ElSSLSocket object has a property called 'Socket' so the underlying socket is a normal .NET socket.

My question, is there some kind of event that I can subscribe to so that I can see when the socket disconnects uncleanly? I have a client server connection which often will disconnect. It is hard to see if it is the client or the server, and hard to see the actual cause.

I am pretty sure that there is some logic problems in the application, i.e. this is not caused by network outages or anything like that.

I should also let you know that the disconnections I get are this error,

SocketException - An existing connection was forcibly closed by the remote host

ErrorCode - 10054

If there was any way that I could actually find the cause of that?

like image 577
peter Avatar asked Jun 22 '10 03:06

peter


1 Answers

Detecting half-open connections is one of the classic socket programming scenarios. The Socket.Connected property is completely useless, and Socket.Poll does not work for every situation. It's best to actually send a "heartbeat" message every few seconds.

10054 is one of the common errors. It means the connection has been lost (not cleanly closed). It can be caused by the remote computer shutting down, the remote process exiting, or even an intermediate router being rebooted.

like image 70
Stephen Cleary Avatar answered Sep 23 '22 01:09

Stephen Cleary