本文介绍了的TcpListener:我如何检测客户端断开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我怎么能当一个客户端从的TcpListener断开检测? 我的客户中的每一个单独的线程来处理。 解决方案 看的 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:我如何检测客户端断开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 06:53
查看更多