1

In a client server application a client is connected to server, now if client disconnects then I want it must trigger an event on the server notifing that it is disconnected. I can achieve this using timer or some loop and a different thread. This thread continuously checks the connection of the client. That is it. But it is something like a polling mechanism. I want to write something like interrupt mechanism. I mean I don't want to use timer or loop. When a client disconnects willingly or unwillingly either from client side or server side my server should notify that client is disconnected. Please dont tell about how to make custom events.

(I have asked the same question few minutes ago but could not find the response properly and question is closed by the forum...)

Will Dean
  • 37,648
  • 10
  • 84
  • 116
himanshu
  • 417
  • 5
  • 18

1 Answers1

1

It's not clear whether you're talking about ASP.NET (or other web stuff), using sockets for direct communication, remoting, WCF or something else. Please clarify if you can.

It seems you're talking about polling vs. event-driven systems. The exact implementation and details depend on the question above. "Please dont tell about how to make custom events" doesn't make much sense: you don't seem to want to poll and depending on the situation, you might have to create a custom event.


Update given the fact you're working with sockets:

There are no events that are fired, with sockets it will simply be that a read, write, etc call will fail with a SocketException.

Please see here for more information:

Instantly detect client disconnection from server socket

You could create your own event for this, if your socket is handled in another thread for example, your thread method could look something like this:

bool running = true;
while (running)
{
    try
    {
        // do socket work here - read/write/etc
    }
    catch (SocketException)
    {
        // something happened, e.g. client disconnected. stop thread running.
        // you could fire your 'Disconnected' event here.
        running = false;
    }
}

Basically, when a client disconnects socket methods will throw SocketException, and you can check the SocketError property of the exception object for the reason. It will be one of these:

http://msdn.microsoft.com/en-us/library/system.net.sockets.socketerror.aspx

These results indicate a disconnected client: ConnectionReset, NotConnected ... but you'll pretty much have to write-off the whole socket if you get any SocketException.

Community
  • 1
  • 1
Kieren Johnstone
  • 38,866
  • 13
  • 82
  • 137
  • using sockets. Actually i dont want to keep server busy in executing the timer or loop to keep track the client connection. So need something which is executed only when any client disconnects. Example: In your house you wont go again and again to check your door that someone is there or not. Its simple when anyone is there, he/she must knock and only then u must go to open door. In the same way if anyone goes from your house he/she notify u that i am leaving. Its not fair to watch ur family member presence in the room again and again. – himanshu Apr 04 '11 at 10:28
  • There is no 'disconnected' event unfortunately, please see my answer. – Kieren Johnstone Apr 04 '11 at 10:36