Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, I'm learning programming and I need help with my new problem. I want to write a server to my app. Where is my problem? So, when my server recives message from anyone client, it should sends replay to all clients. I don't know how my clients should waiting for this replay and how all threeds in server send same replay to clients.

Sory for my English :)
To better see my problem I do animation:
click
Posted

Using a separate thread per client is a very bad idea. I will lead you nowhere. Why?

Please see my past answer to a related question, it can give you a right idea:
Multple clients from same port Number[^].

—SA
 
Share this answer
 
I make samething like this
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace server
{
    class server
    {
        List<tcpclient> clientList;
        TcpListener listener;

        public server(int port)
        {
            clientList = new List<tcpclient>();
            listener = new TcpListener(System.Net.IPAddress.Any, port);
            listener.Start();

        }

        public void listengForClient()
        {
            Thread newClient = new Thread(listeningForNewClient);
            newClient.Start();
        }

        private void listeningForNewClient()
        {
            while (true)
            {
                TcpClient client = default(TcpClient);
                client = listener.AcceptTcpClient();

                lock (clientList)
                {
                    clientList.Add(client);
                    Console.WriteLine(" >> Adding new client... Client count: " + clientList.Count);
                }
            }
        }

        public void receiveDateFromClients()
        {
            Thread receiver = new Thread(receiveDate);
            reciver.Start();
        }

        private void receiveDate()
        {
            while (true)
            {
                lock(clientList)
                for (int i = 0; i < clientList.Count; i++)
                {
                    if (clientList[i].Connected)
                    {
                        if (clientList[i].Available > 0)
                        {
                            // get message form i-client
                            Console.WriteLine(" >> >> >> Message from client: " + i);
                            
                        }
                    }
                    else
                    {
                        // client disconnected so it is removing
                        Console.WriteLine(" >> >> Removing client: " + i);
                        clientList.RemoveAt(i);
                        i--; if (i < 0) i = 0;
                    }
                }
            }
        }
    }
}
</tcpclient></tcpclient>


I know receiving date is bad, becaouse it uses 95% CPU, but I don't know how do it better...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900