从StreamReader中检索超过65536个字符

时间:2013-02-02 14:15:57

标签: c# buffer streamreader

  

可能重复:
  C# Socket TCP Send, messages getting “stuck”

我正在尝试在此TCP客户端上收到大量消息,但我只能获得第一个65536个字符。

我看到有人说maxReceivedMessageSize,但似乎不是TCP。也找不到如何使用它。

我正在使用Windows窗体。

这是我的代码:

Stream Reader声明:

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                client.Connect(textBox1.Text, Convert.ToInt16(textBox2.Text));

                writer = new StreamWriter(client.GetStream());
                reader = new StreamReader(client.GetStream());
                network = client.GetStream();
                connected = true;
                MessageBox.Show("Connected successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not connect to server:" + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

阅读代码:

   private void ReadDir(object dir)
    {
        if (isConnected())
        {
            JObject o = new JObject();
            string message = null;

            o.Add("comando", (int)Command.READ_DIR);
            o.Add("dir", (string)dir);

            writer.Write(o.ToString());
            writer.Flush();

            Thread.Sleep(500);
            if (client.Available > 0)//returns 65536 or less only
            {
                char[] buffer = new char[client.Available];
                reader.Read(buffer, 0, buffer.Length);
                message = new string(buffer);
            }


            if (message != null)
            {

                JArray array = JArray.Parse(message);

                textBox3.Text = (string)dir;
                listView1.Items.Clear();

                for (int i = 0; i < array.Count; i += 5)
                {
                    ListViewItem lvi = new ListViewItem((string)array[i]);
                    lvi.SubItems.Add((string)array[i + 1]);
                    lvi.SubItems.Add((string)array[i + 2]);

                    Debug.WriteLine((string)array[i + 3]);
                    if ((Int64)array[i + 3] > 10 * 1024 * 1024)
                    {
                        lvi.BackColor = Color.LightGreen;
                    }

                    if ((bool)array[i + 4] == true)
                    {
                        lvi.BackColor = Color.Yellow;
                    }

                    listView1.Items.Add(lvi);
                }
            }
        }
    }

如何使用我现在使用的相同方法一次阅读较大的消息?

0 个答案:

没有答案