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

  • SEARCH
  • Home
  • 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 6897869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:13:25+00:00 2026-05-27T07:13:25+00:00

I created a client and server that connect to each other. The server is

  • 0

I created a client and server that connect to each other. The server is capable of sending the message a client sent back to the client (after processing)… however… if there are two clients connected, it only sends the message back to the client that sent the message in the first place (lol…)

How would i fix this so that it sends a message from any client to every client?

I used the example below as a starting point to get a connection between the client and server:

client server communication

when i try the following my program freezes up:

SERVER:

 private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client; 
        clientStream = tcpClient.GetStream();


            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }
            }


                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                foreach (TcpClient c in ListOfClients)
                {
                System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));

                clientStream.Write(message, 0, message.Length);


            }

CLIENT:

private void boxChatArea_KeyPress(object sender, KeyPressEventArgs e)
    {
        char[] contentForServer = null;
        boxChatArea.MaxLength = 4000; 


        if (e.KeyChar == (char)Keys.Enter)
        {

            client = new TcpClient();

            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), MainWindow.port);
            client.Connect(serverEndPoint);
            clientStream = client.GetStream();


            contentForServer = boxChatArea.Text.ToCharArray();
            byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(contentForServer);
            clientStream.Write(bytesToSend, 0, bytesToSend.Length);
            clientStream.Flush();
            boxChatArea.Text = null;

            StartListening(); 

        }
    }


    public void StartListening()
    {

        HandleServerComm(client);
    }




    private void HandleServerComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        bytesRead = 0;

            try
            {
                //FREEZES HERE - it doesn't freeze here without the loop that we added within the server... 
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                //break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
            }

            if (bytesRead != 0)
            {
                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                string text = (encoder.GetString(message, 0, message.Length));

                if (enterCount >= 1)
                {
                    displayBoxChatArea.AppendText(Environment.NewLine);
                    displayBoxChatArea.AppendText(text);
                    //displayBoxChatArea.Text = text;
                    Application.DoEvents();
                }
                else
                {
                    displayBoxChatArea.Text = text;
                    Application.DoEvents();
                }
            }
        enterCount++; 
        tcpClient.Close();

    }
  • 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-27T07:13:25+00:00Added an answer on May 27, 2026 at 7:13 am

    You should take a list of connected clients.
    Try something like this:

    List<TcpClient> clients = new List<TcpClient>();
    
    private void ListenForClients()
    {
         this.tcpListener.Start();
    
         while (true)
         {
              //blocks until a client has connected to the server
              TcpClient client = this.tcpListener.AcceptTcpClient();
              if(!clients.Contains(clients))
                  clients.Add(client);
         }
    }
    

    EDITED after user comments:
    You did your sending function wrong: you must first get just the message from one client, then send to everyone.

    clientStream = tcpClient.GetStream(); // Not in foreach loop !!
    // ...
    System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead)); 
    foreach(TcpClient client in clients)
    {
        // Send message to client
        st = client.GetStream();
        st.Write(message, 0, message.Length);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Client/Server application with the IdTCPServer component. The clients connect and
So I installed VisualSVN Server and TortoiseSVN client. And I created a user on
I am struggling with the following scenario: an XML-message is created client-side and digitally
This involves two automated unit tests which each start up a tcp/ip server that
I'm writing a client/server application that requires the server needs to be able to
From this rpg socket tutorial we created a socket client in rpg that calls
I want to create light object data-package to pass between client and server applications.
I want to create an application which will have a client and server components.
I am trying to create a simple chat client using the red5 media server,
I want to create SOCKET chat(server + clients) with private messaging. When client write

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.