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

Posted by menacheb on Server Fault See other posts from Server Fault or by menacheb
Published on 2010-05-20T09:45:17Z Indexed on 2010/05/20 9:50 UTC
Read the original article Hit count: 190

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,

© Server Fault or respective owner

Related posts about c#

Related posts about client-server