0_______0
0_______0

Reputation: 557

issue with multiple clients and getting a specific stream

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?

Upvotes: 1

Views: 695

Answers (1)

Tomislav Markovski
Tomislav Markovski

Reputation: 12356

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.

Upvotes: 1

Related Questions