简体   繁体   中英

Checking NetworkStream content with StreamReader

and greetings to all members. I'm working on a small embedded data acquisition hardware which collects data periodically and runs a small embedded webserver. If requested the server replies with a very basic html page along with the collected data in 128 bytes TCP chunks. The size of the transferred data is maximum 64K.

The embedded TCP/IP stack is quite thin and the MCU is not the fastest, so I'd like the requester application to have a progress indicator (a progress bar or similar). Since the length of data array to be transferred is constantly changing the server should insert the actual data length in the beginning of the data array. The requester app should read this length information and update the progress of the progressbar. With the following code I can receive the server content but only can access the data when the transfer is completed:

        TcpClient msnTcpConn = new TcpClient();
        try
        {
            msnTcpConn.Connect(HostName, 80);

            if (msnTcpConn.Connected)
            {

                string msg = "";

                NetworkStream netStream = msnTcpConn.GetStream();
                System.IO.StreamWriter sw = new System.IO.StreamWriter(netStream);
                System.IO.StreamReader sr = new System.IO.StreamReader(netStream);

                string req = "";
                req += "GET / HTTP/1.0\r\n";
                req += "Host: " + HostName + "\r\n";
                req += "\r\n";

                sw.Write(req);
                sw.Flush();
                numa = 0;
                peek = 0;
                while (sr.Peek()>=0)
                {
                    peek = sr.Peek();
                    numa++;
                    textBox1.Text = Convert.ToString(numa);
                    msg += sr.ReadLine();;
                }                 

                webBrowser1.DocumentText = msg;
                msnTcpConn.Close();
            }
            else
            {
                MessageBox.Show("Not connected.");
            }
        }
        catch (UriFormatException UriExc)
        {
            MessageBox.Show("Error!");
        }  

In the above code, textBox1 is not getting refreshed in the while loop only when the loop is exited. Thus, I can't check the received data in time to extract the data size which is needed to update the progress indicator. I'm a beginner in .NET networking so maybe there is a better way to do this thing so any tip or help would be greatly appreciated. Thank you.

Edited to address the OPs comments:

You would need to move your netowrk reading code to a background tread (System.Threading.Thread) and then call your UI update code via Dispatcher.BeginInvoke.

This is a threading issue. The UI will not update until the code executed on the UI thread is complete. You will want to move your code that reads from the NetworkStream to another thread. You will need to make sure your code that writes to your TextBox does so on the UI thread. You can do this with BeginInvoke if this is a WinForms app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM