Detecting socket disconnection (Async?)

  • Thread starter Thread starter Adam Clauss
  • Start date Start date
A

Adam Clauss

There seems to be various methods to determine when the remote client disconnects, but all of them I have seen are Synchronous.
AKA: Right before you try to send or receive data, check.

Is there no way to do raise an event for this? That way you know as soon as the client disconnects?

There are events for Accepting, Connecting, Receiving, Sending... why not one for disconnecting?
 
Adam,

There are two types of disconnects; when the client explicitly closes
the
connection or when the connection is lost due to a network error (connect
went down, etc).

When the client explicitly closes the connection, your recieve callback will
be called and your will recieve zero bytes - when this happens you know that
the client is question has disconnected. To check for network errors you
will
have to catch the SocketException which you will be getting :)

HTH,

//Andreas

Adam Clauss said:
There seems to be various methods to determine when the remote client
disconnects, but all of them I have seen are Synchronous.
 
Hmm... OnReceive gets called for OnDisconnect. Well, I suppose it works, just does not seem very logical.

Thanks!
 
Adam,

It's all documented in the docs and its just a wrapper for the
underlaying WinSock
library. I felt the same at first but when you think about it, it makes
sence - how would
the socket notify the remote socket it disconnected? By sending information
and leting
the remote socket know. The most efficient notification message it can send
it an empty
one.

Agreed that a nice wrapped event where the Socket class checked the recive
on
triggered the event if it was zero byte would be nice - but the async call
runs on a
seperate thread and when you call Begin..() you pass the adress of the
return function
so it jumps directly to the callback and not back to the socket it self
which then forward
the result.

HTH,

//Andreas

Adam Clauss said:
Hmm... OnReceive gets called for OnDisconnect. Well, I suppose it works,
just does not seem very logical.
 
Back
Top