简体   繁体   English

使用StreamReader检查NetworkStream内容

[英]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. 如果请求,服务器将回复一个非常基本的html页面以及128字节TCP块中收集的数据。 The size of the transferred data is maximum 64K. 传输数据的大小最大为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). 嵌入式TCP / IP堆栈非常薄,MCU不是最快的,所以我希望请求者应用程序有一个进度指示器(进度条或类似的)。 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. 在上面的代码中,只有在退出循环时,textBox1才会在while循环中刷新。 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. 我是.NET网络的初学者,所以也许有更好的方法来做这件事,所以任何提示或帮助将不胜感激。 Thank you. 谢谢。

Edited to address the OPs comments: 编辑解决OP的意见:

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. 您需要将netowrk读取代码移动到后台(System.Threading.Thread),然后通过Dispatcher.BeginInvoke调用UI更新代码。

This is a threading issue. 这是一个线程问题。 The UI will not update until the code executed on the UI thread is complete. 在UI线程上执行的代码完成之前,UI不会更新。 You will want to move your code that reads from the NetworkStream to another thread. 您将希望将从NetworkStream读取的代码移动到另一个线程。 You will need to make sure your code that writes to your TextBox does so on the UI thread. 您需要确保写入TextBox的代码在UI线程上执行此操作。 You can do this with BeginInvoke if this is a WinForms app. 如果这是一个WinForms应用程序,您可以使用BeginInvoke执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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