How to know when the Client is disconnected from Server in C#?

Posted by menacheb on Stack Overflow See other posts from Stack Overflow or by menacheb
Published on 2010-05-20T09:45:17Z Indexed on 2010/05/21 9:30 UTC
Read the original article Hit count: 242

Filed under:
|

Hi,
I have a Client-Server program in C#.
Here is the Server's code:

...
String dataFromClient =  "";
NetworkStream networkStream;
TcpClient clientSocket;
bool transfer = true;
...
while (transfer)
    {

         networkStream = clientSocket.GetStream();
         networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
         dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
         dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
         ....
     }

I want to make a condition that stop the loop when the Client is disconnected.
How can I do that?
Many thanks,

© Stack Overflow or respective owner

Related posts about c#

Related posts about client-server