Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET socket Networking
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></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
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 25 Dec 2011
Copyright © CodeProject, 1999-2014
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