Exemplo n.º 1
0
        private void WaitForClientRequest(TcpClient newClient)
        {
            NetworkStream stream = newClient.GetStream();

            byte[] messageFromClient = new byte[2048];
            _running = true;

            while (_running)
            {
                int byteRead = 0;
                try
                {
                    byteRead = stream.Read(messageFromClient, 0, 2048);

                    if (byteRead == 0)
                    {
                        break;
                    }
                }
                catch (Exception)
                {
                    break;
                }

                ASCIIEncoding encoder = new ASCIIEncoding();
                Console.WriteLine("serveur recieves:\n\t" + encoder.GetString(messageFromClient, 0, byteRead) + "\n");

                try
                {
                    byte[] response = _moduleLister.ProcessDataFromClient(messageFromClient, byteRead);
                    stream.Write(response, 0, response.Length);
                    stream.Flush();
                }
                catch (Exception)
                {
                    Console.WriteLine("impossible d'envoyer les données au client");
                }
            }
            newClient.Close();
        }