首页 \ 问答 \ TcpListener:检测客户端断开连接,而不是客户端暂时不发送任何数据(TcpListener: Detecting a client disconnect as opposed to a client not sending any data for a while)

TcpListener:检测客户端断开连接,而不是客户端暂时不发送任何数据(TcpListener: Detecting a client disconnect as opposed to a client not sending any data for a while)

我正在寻找在使用TcpListener时如何检测“客户端断开连接”。

所有答案似乎都与此类似: TcpListener:如何检测客户端断开连接?

基本上,从流中读取,如果Read()返回0,则客户端已断开连接。

但这是假设客户端在发送的每个数据流之后断开连接。 我们在TCP连接/断开开销既缓慢又昂贵的环境中运行。

我们建立连接然后发送一些请求。

伪代码:

client.Connect();
client.GetStatus();
client.DoSomething();
client.DoSomethingElse();
client.AndSoOn();
client.Disconnect();

Connect和Disconnect()之间的每次调用都会向服务器发送数据流。 服务器知道如何分析和处理流。

如果让TcpListener在没有断开连接的情况下读取循环,则会读取并处理所有消息,但在客户端断开连接后,服务器无法知道它并且它永远不会释放客户端并接受新客户端。

var read = client.GetStream().Read(buffer, 0, buffer.Length);

if (read > 0) 
{
   //Process
}

如果我让TcpListener在读取== 0时删除客户端,它只接受第一个数据流,只是在之后立即丢弃客户端。

当然这意味着新客户可以连接。

呼叫之间没有人为的延迟,但就计算机时间而言,两次呼叫之间的时间当然是“巨大的”,因此总会有一段时间读取== 0,即使这并不意味着客户端已经或应该断开连接。

var read = client.GetStream().Read(buffer, 0, buffer.Length);

if (read > 0) 
{
   //Process
}
else
{
   break;   //Always executed as soon as the first stream of data has been received
}

所以我想知道...有没有更好的方法来检测客户端是否已断开连接?


I was looking how to detect a 'client disconnect' when using a TcpListener.

All the answers seem to be similar to this one: TcpListener: How can I detect a client disconnect?

Basically, read from the stream and if Read() returns 0 the client had disconnected.

But that's assuming that a client disconnects after every single stream of data it sent. We operate in environments where the TCP connect/disconnect overhead is both slow and expensive.

We establish a connection and then we send a number of requests.

Pseudocode:

client.Connect();
client.GetStatus();
client.DoSomething();
client.DoSomethingElse();
client.AndSoOn();
client.Disconnect();

Each call between Connect and Disconnect() sends a stream of data to the server. The server knows how to analyze and process the streams.

If let the TcpListener read in a loop without ever disconnecting it reads and handles all the messages, but after the client disconnects, the server has no way of knowing that and it will never release the client and accept new ones.

var read = client.GetStream().Read(buffer, 0, buffer.Length);

if (read > 0) 
{
   //Process
}

If I let the TcpListener drop the client when read == 0 it only accepts the first stream of data only to drop the client immediately after.

Of course this means new clients can connect.

There is no artificial delay between the calls, but in terms of computer time the time between two calls is 'huge' of course, so there will always be a time when read == 0 even though that does not mean the client has or should be disconnected.

var read = client.GetStream().Read(buffer, 0, buffer.Length);

if (read > 0) 
{
   //Process
}
else
{
   break;   //Always executed as soon as the first stream of data has been received
}

So I'm wondering... is there a better way to detect if the client has disconnected?


原文:https://stackoverflow.com/questions/11662598
更新时间:2022-05-15 08:05

最满意答案

我建议利用XMPP并使用ejabberd作为您的服务器。 在客户端(Web端),您可以使用Strophe.js库与您的ejabberd服务器“通信”。 它可能是最活跃和最新的JavaScript库,开发人员通常可以帮助回答聊天室的问题(Jabber:sleek@conference.jabber.org)。

XMPP与谷歌聊天,Facebook聊天等协议相同,如果需要,可以使用和集成它们。 我不能推荐足够的阅读XMPP:The Definitive Guide作为项目的起点,以便完全理解协议。

我推荐给客户端(web)的另一本书是使用JavaScript和jQuery的Professional XMPP Programming ,尽管一些示例代码有一些小错别字。

祝你好运!

编辑:另外,我应该提到“Jabber”和“XMPP”在聊天世界中经常互换使用 - 所以当你开始阅读时,你不会感到困惑。


I would recommend leveraging XMPP and using ejabberd as your server. On the client side (web side), you could use the Strophe.js library to "talk to" your ejabberd server. It is possibly the most active and up-to-date JavaScript library and the developers are usually available to help answer questions on their chatroom (Jabber: sleek@conference.jabber.org).

XMPP is the same protocol that Google chat, Facebook chat, etc. all use and integration with them is possible if desired. I can't recommend enough reading XMPP: The Definitive Guide as a jumping off point for your project in order to fully understand the protocol.

Another book I recommend for the client (web) side is Professional XMPP Programming with JavaScript and jQuery, though some of the example code has minor typos.

Good luck!

Edit: Also, I should mention that "Jabber" and "XMPP" are used interchangeably very often in the chat world - so you don't get confused when you're starting out with your reading.

相关问答

更多
  • 从评论到其他答案来看,我会告诉你为什么,还有一点是什么,但不给你一个解决方案,因为我看到“相关”侧栏中有很多解决方案。 你将不得不选择正确的,并知道“为什么”你将能够做出一个受过教育的决定。 为了让聊天感觉正确,答案必须有一些直接性。 随着时间的推移,用户会注意到一秒钟的滞后时间,并给人一种不合时宜的感觉。 要在浏览器中立即或“实时”响应,需要持久连接,以便在新信息进入时立即显示。 由于HTTP的请求/响应规范,浏览器中的持久连接很困难。 有一些规范正在努力为浏览器带来持久连接,但这些浏览器并不普遍。 将来 ...
  • 如果我正确理解你,你想让客户端连接到你的服务器,并通过服务器通过ajax发送消息给对方。 这是可能的,有两种方法可以做到这一点。 简单的方法是让客户端每隔几秒轮询一次以检查另一端发布的新消息。 缺点是消息不能即时传递。 我认为这是在rails书中找到的一个例子。 更复杂的方法是保持开放连接并在服务器收到消息后立即将消息发送给客户端。 要做到这一点,你可以使用像剑圣一样的东西 我想补充说的是,尽管后者有效,但它不是http的意思,它有点破解,但是,嘿,无论什么工作完成。 这个工作的例子是使用剑圣衍生物的rai ...
  • 首先,我认为你不太可能找到一个收缩包解决方案来完成所有这些在Clojure(除了以通过interop使用的Java库的形式)。 什么是Clojure的标准Web堆栈包括许多图书馆,人们以各种方式混合和匹配(因为他们乐意地完全兼容)。 1 以下是您可能会发现的一些构建块的列表: Ring - Clojure的基本HTTP请求处理库; 我知道的所有其他webby库(用于写入路由和c。)与Ring兼容。 Ring正在积极开发,拥有强大的社区,写得非常好,并且有一个很好的SPEC文档,详细说明其设计理念。 这篇博文 ...
  • 使用Nodejs \ ExpressJS, socketIO真的最好。 NodeJS是最好的,或者您可以使用带有signalR的 asp.net,或者您也可以尝试使用firebase 。 Use Nodejs\ExpressJS, socketIO it really best. NodeJS is best or you can use asp.net with signalR or you can also try firebase.
  • 我至少会学习基本的servlet生命周期和API。 正如Joel所说, 抽象是有漏洞的 ,这适用于框架 - 所有框架不仅仅是Web框架 - 同样适用。 你会更好地装备使用框架,知道为什么它是好的,以及如果你理解底层技术,它是试图抽象的东西,它会如何帮助你。 至于Struts,我会避开STruts 1.现在它已经很古老了。 仍然有很多代码,但我现在不认为它是最佳实践,尤其是对其(超)继承的使用。 Struts 2实际上是一个基于Webwork的完全不同的框架。 有很多其他的MVC框架。 我个人喜欢Spring ...
  • WebSDK足够新了。 我们正在处理其文档。 但是,在这里,我将向您展示一些代码片段,了解如何创建一对一聊天。 如您所知,QuickBlox使用XMPP服务器作为聊天服务。 WebSDK没有XMPP API的包装器,因此您应该包含其他XMPP JS库。 对于我们的示例,我们建议使用Strophe.js( http://strophe.im/strophejs/ ) 让我们开始: 1)包括您的xmpp js库和WebSDK
  • 免责声明:我是IBM的网络和移动顾问。 听起来我首先需要了解一些关于Dojo Mobile的内容。 Dojo Mobile是Dojo工具包的特定子集,用于构建移动应用程序,具有特定的移动优化UI小部件等。本教程与任何入门一样好: http://dojotoolkit.org/documentation/tutorials/1.6/mobile/tweetview/getting_started/ 您可能还会发现Dojo Mobile展示对于了解Dojo Mobile的一些功能非常有用: http://dem ...
  • 我建议利用XMPP并使用ejabberd作为您的服务器。 在客户端(Web端),您可以使用Strophe.js库与您的ejabberd服务器“通信”。 它可能是最活跃和最新的JavaScript库,开发人员通常可以帮助回答聊天室的问题(Jabber:sleek@conference.jabber.org)。 XMPP与谷歌聊天,Facebook聊天等协议相同,如果需要,可以使用和集成它们。 我不能推荐足够的阅读XMPP:The Definitive Guide作为项目的起点,以便完全理解协议。 我推荐给客户 ...
  • 您是否考虑过socket.io以获得真正的跨浏览器套接字? 否则你可以使用已经为此开发的东西,而不是重新发明轮子,就像已经存在的XMPP JS库一样。 如果你想自己实现一些东西,你最好的镜头是ajax长轮询,没有你的页面不断重新加载,而是提供某种web服务,并使用JS(或jQuery)与它进行通信,并仅传输聊天 - 相关数据。 Have you considered socket.io in order to get a real cross-browser socket? Otherwise you co ...
  • 我建议使用strophejs和书籍Professional XMPP with jquery和Javascript by Jack Moffitt。 strophejs非常低级但功能非常强大, 书籍代码样本带你去任何你想去的地方。 而且,补充克里斯托弗的评论, speeqe.com部分是由莫菲特写的并使用strophejs! I recommend using strophejs and the book Professional XMPP with jquery and Javascript by Jac ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)