Click here to Skip to main content
12,067,646 members (38,570 online)
Rate this:
 
Please Sign up or sign in to 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 Smile | :)
To better see my problem I do animation:
click
Posted 24-Dec-11 15:37pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I make samething like this
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>

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 25 Dec 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100