TcpListener: How can I detect a client disconnect?

别说谁变了你拦得住时间么 提交于 2020-01-01 05:14:06

问题


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

Each of my clients is handled in a separate thread.


回答1:


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.



来源:https://stackoverflow.com/questions/1202154/tcplistener-how-can-i-detect-a-client-disconnect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!