的TcpListener:我如何检测客户端断开? [英] TcpListener: How can I detect a client disconnect?

查看:130
本文介绍了的TcpListener:我如何检测客户端断开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能当一个客户端从的TcpListener断开检测?



我的客户中的每一个单独的线程来处理。


< DIV CLASS =h2_lin>解决方案

看的 NetworkStream.Read ,或取决于您使用也许在你的代码什么的 TCPClient.GetStream 是比较合适的,但基本上是相同的答案。



从文档




这方法将数据读入缓冲区
参数和返回成功读取的
字节数。如果没有数据可供读取
,读取方法
返回0读操作为
读取的数据量为可用,最多字节由指定的
号大小
参数。 如果远程主机关闭
下来的连接,以及所有可用
数据已经被接收,读
法立即完成和
返回零字节。



How can I detect when a client disconnects from the TcpListener ?

Each of my clients is handled in a separate thread.

解决方案

Look at NetworkStream.Read, or depending on what you are using in your code maybe TCPClient.GetStream is more appropriate, but basically the same answer.

From the docs:

This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the Read method returns 0. The Read operation reads as much data as is available, up to the number of bytes specified by the size parameter. If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes.

这篇关于的TcpListener:我如何检测客户端断开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆