收录日期:2019/05/20 13:08:33 时间:2009-07-29 18:29:14 标签:c#,vb.net,multithreading,tcpclient,tcplistener

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.