How do i judge when the NetWorkStream finishes by using .net TcpClient to communicate

Posted by Hwasin on Stack Overflow See other posts from Stack Overflow or by Hwasin
Published on 2010-03-18T10:40:05Z Indexed on 2010/03/18 10:41 UTC
Read the original article Hit count: 480

Filed under:
|
|
|

I try to use stream.DataAvailable to judge if it is finished,but sometimes the value is false but after a little while it is true again,i have to set a counter and judge the end by the symbol '>' like this

int connectCounter = 0;

            while (connectCounter < 1200)
            {
                if (stream.DataAvailable)
                {
                    while (stream.DataAvailable)
                    {
                        byte[] buffer = new byte[bufferSize];
                        int flag = stream.Read(buffer, 0, buffer.Length);
                        string strReadXML_t = System.Text.Encoding.Default.GetString(buffer);
                        strReadXML = strReadXML + strReadXML_t.Replace("\0", string.Empty);       
                    }

                    if (strReadXML.Substring(strReadXML.Length - 1, 1).Equals(">"))
                    {
                        break;
                    }
                }
                Thread.Sleep(100);
                connectCounter++;
            }

is there any good methord to deal with it?Thank you!

© Stack Overflow or respective owner

Related posts about c#

Related posts about tcpclient