简体   繁体   English

如何跟踪该客户端已断开连接?

[英]how to trace that client is disconnected?

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...) (几分钟前我曾问过同样的问题,但找不到正确的答案,问题已由论坛关闭...)

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. 目前尚不清楚您是在谈论ASP.NET(或其他Web东西),使用套接字进行直接通信,远程处理,WCF还是其他。 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 . 没有引发任何事件,对于套接字,只是由于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. 基本上,当客户端断开套接字方法时,将抛出SocketException ,并且您可以检查异常对象的SocketError属性以了解原因。 It will be one of these: 将是以下之一:

http://msdn.microsoft.com/en-us/library/system.net.sockets.socketerror.aspx http://msdn.microsoft.com/zh-cn/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 . 这些结果表明客户端已断开连接:ConnectionReset,NotConnected ...,但是如果收到任何SocketException则几乎必须注销整个套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM