Bruno Klein
Bruno Klein

Reputation: 3367

How to retrieve large strings over TCP using Streamreader

I'm trying to receive large strings over TCP, I tried various methods, but none of them worked as good as this one (which is quite simple actually).

public partial class MyClass : Form
{
    Int64 counter;
    StreamWriter writer;
    StreamReader reader;

    public MyClass(object streamIn, object StreamOut)
    {
        InitializeComponent();
        richTextBox1.BackColor = Color.Black;
        richTextBox1.ForeColor = Color.Gray;


        writer = (StreamWriter)streamIn;
        reader = (StreamReader)StreamOut;

    }

    private void button1_Click(object sender, EventArgs e)
    {
            JObject o = new JObject();
            char[] buffer = new char[1024];
            int count = buffer.Length;

            o.Add("comando", 15);
            o.Add("filename", textBox2.Text);
            o.Add("param", textBox3.Text);

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

            richTextBox1.Text = reader.ReadToEnd();
    }
}

The problem using this is that I have to close the stream on the other end, in order for this to read. There is any way I can use reader.ReadToEnd() without having to close the stream on the other end after sending and therefore closing the connection between client-server?

Upvotes: 1

Views: 213

Answers (1)

MarcF
MarcF

Reputation: 3299

Checkout the Basic Example for the network library networkcomms.net which is covered in the Getting Started article. Although this is a console example it allows you to send arbitrary length strings.

Your example looks like it might be a winform application. There is also a WPF chat application example if that is of interest.

Upvotes: 1

Related Questions