Example #1
0
        public void Handle()
        {
            NetworkStream stream = null;

            try
            {
                stream = _client.GetStream();
                while (true)
                {
                    dynamic request = Messager.Recive(stream);
                    _logger.Log($"Was recived mesage : {request}");
                    if (request["stop"] == true)
                    {
                        return;
                    }
                    var words      = JsonConvert.DeserializeObject <string[]>(request["words"].ToString());
                    var files      = _index[words];
                    var answer     = new { files };
                    var jsonAnswer = Messager.Send(stream, answer);
                    _logger.Log($"Was sended answer : {jsonAnswer}");
                }
            }
            catch (JsonException e)
            {
                var answer = new { error = "Invalid body" };
                Messager.Send(stream, answer);
                _logger.Log(e.Message);
            }
            finally
            {
                stream?.Close();
                _client?.Close();
                _logger.Log("Client disconected");
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            TcpClient     client = null;
            NetworkStream stream = null;
            Logger        logger = new Logger();

            try
            {
                client = new TcpClient("127.0.0.1", 5001);
                stream = client.GetStream();

                do
                {
                    var input = GetWords();
                    Messager.Send(stream, new { words = input });
                    var answer = Messager.Recive(stream);
                    Console.WriteLine(answer);
                } while (WantToContinue());
                Messager.Send(stream, new { stop = true });
            }
            catch (Exception e)
            {
                logger.Log(e.Message);
            }
            finally
            {
                client?.Close();
                stream?.Close();
            }
        }