Esempio n. 1
0
        public static Hashtable clientsList = new Hashtable(); //Liste des clients

        static void Main(string[] args)
        {
            Int32       port         = 8888;
            IPAddress   localAddr    = IPAddress.Parse("127.0.0.1");
            TcpListener serverSocket = new TcpListener(localAddr, port); //Ecoute de connexion clients sur l'ip/port
            TcpClient   clientSocket = default(TcpClient);               //Client
            int         counter      = 0;

            serverSocket.Start(); //On lance l'écoute serveur
            Console.WriteLine("-------Le serveur de Chat est lancé-------");
            counter = 0;
            while ((true))
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient(); //Accept les requêtes de connexion

                byte[] bytesFrom      = new byte[10025];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();          //On récupère le flux du client
                networkStream.Read(bytesFrom, 0, bytesFrom.Length);              //(int)clientSocket.ReceiveBufferSize
                dataFromClient = System.Text.Encoding.UTF8.GetString(bytesFrom); //On convertit le flux en string UTF8
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                clientsList.Add(dataFromClient, clientSocket);

                broadcast(dataFromClient + " s'est connecté ", dataFromClient, false); //On envoie à tout le monde le message "s'est connecté"

                Console.WriteLine(dataFromClient + " s'est connecté ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8888);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            counter = 0;
            while (true)
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, Convert.ToString(counter));
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }