1

I have a simple multithreaded C# server and client. When just one client is connected I can interact with it fine, but when two or more are connected, it seems I am using the last NetworkStream. What I'd like to be able to do is give an input command that specifies the stream to read and write to. So, for example, the first client is "Client 1" and the second client is "Client 2." I'd just type "Client 2" into my command textbox and it will get the stream for the second client.

The problem is, I don't know how to assign the text to the clients. Here is the relevant code from the server:

    private void ClientThread(Object client)
    {
        NetworkStream networkStream = ((TcpClient)client).GetStream();
        Dictionary<int, NetworkStream> myClients = new Dictionary<int, NetworkStream>(); // This didn't work.
        myClients.Add(counter, ((TcpClient)client).GetStream()); // Wouldn't write.
counter = counter + 1;
        streamReader = new StreamReader(networkStream);
        streamWriter = new StreamWriter(networkStream);
        strInput = new StringBuilder();          
        while (true)
        {
            try
            {
                strInput.Append(streamReader.ReadLine());
                strInput.Append("\r\n");
            }
            catch (Exception error)
            {
                break;
            }
            Application.DoEvents();
            DisplayMessage(strInput.ToString());
            strInput.Remove(0, strInput.Length);
        }
    }

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            if (e.KeyCode == Keys.Enter)
            {                 
                //ListView.SelectedListViewItemCollection stuff = listView1.SelectedItems;
                //ip is displayed in listView1, if I could also bind the stream for the ip 
                //to it and select it, that would be cool.
                {
                    strInput.Append(textBox2.Text.ToString());
                    streamWriter.WriteLine(strInput);
                    streamWriter.Flush();
                    strInput.Remove(0, strInput.Length);
                    if (textBox2.Text == "cls") textBox1.Text = "";
                    textBox2.Text = "";
                }
            }
        }
        catch (Exception error) { }
    }

So, how can I do this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0_______0
  • 547
  • 3
  • 11

1 Answers1

1
NetworkStream networkStream = myClients[2];
using(streamWriter = new StreamWriter(networkStream))
{
    streamWriter.WriteLine("hello client 2"); // send something to Client 2
}

networkStream = myClients[4];
using(streamWriter = new StreamWriter(networkStream))
{
    streamWriter.WriteLine("hello client 4"); // send something to Client 4
}

You are obviously storing all your client streams into a dictionary. Just load that stream into a StreamWriter and send your data. Make your dictionary myClients class field and then just get your currently active stream like above.

Ivo
  • 8,172
  • 5
  • 27
  • 42
Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
  • OK, so I added a text box and use NetworkStream networkStream2 = myClients[Convert.ToInt32(textBox3.Text)]; to get the stream I want from the dictionary. Every time a new entry is added counter is incremented by one. With just one client, as long as I have the correct number in the box, it works fine. But once another client connects I can't get the previous stream. It acts as if it doesn't exist anymore. What's wrong? – 0_______0 Dec 24 '11 at 22:22
  • @JohnSaunders, indeed, what ivowiblo said. What a way to support the community. – Tomislav Markovski Dec 25 '11 at 02:49
  • 1
    I seem to have hit a wall with my current method. I don't want to release the stream after each message sent, which is what "using" seems to be doing. I want to be able to keep the streams open after the message. And switch between clients whenever I want. Which is why every time I hit enter it checks the number in textBox3 and grabs that stream from the dictionary entry (which only works when one client is connected). Thanks anyways. – 0_______0 Dec 25 '11 at 04:44