Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6968089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:24:23+00:00 2026-05-27T16:24:23+00:00

I have a simple multithreaded C# server and client. When just one client is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:24:23+00:00Added an answer on May 27, 2026 at 4:24 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a simple multithreaded tcp server. The client connects and sends
I have a server that communicates with a client. The server is multithreaded, and
I have to write a multithreaded client and server using Java's socket api. Both
I have simple issue setting a two-way databinding of a checkbox in Silverlight 3.0.
I have a multithreaded application running on Win XP. At a certain stage one
I have a simple question, how to convert this server into multithreded as far
I have implemented a really simple C# Http Server using HttpListener within a windows
I have coded a single threaded client server model which does following: Server loops
I'm implementing a simple TCP client and TCP server in Java at the moment,
Interlocked.Increment seems like among the most standard/simple of operations one would need to perform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.