Example #1
0
        /// <summary>
        /// Обработчик запросов
        /// </summary>
        /// <param name="clientId">Id клиента</param>
        private static void requestsHandler(int clientId)
        {
            IFormatter formatter = new BinaryFormatter();

            // Получить поток клиента
            NetworkStream clientStream = clients[clientId].TcpClient.GetStream();

            // Обработка запросов
            while (true)
            {
                // Получить запрос
                try
                {
                    var request = (RequestTypes)formatter.Deserialize(clientStream);

                    Command command = new DbCommand(context, request, clientStream);
                    command.Execute();
                }
                catch (Exception ex)
                {
                    LoggerEvs.writeLog("Connection closed");
                    clients.Remove(clientId);
                    break;
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            //InitDb();
            Console.Title = "Personal accounting server:2018";
            const int receiveTimeout = 60 * 100000;
            const int sendTimeout    = 60 * 100000;

            LoggerEvs.messageCame += logToConsole;

            const int port = 11000;

            TcpListener tcpListener = new TcpListener(IPAddress.Any, port);

            tcpListener.Start();
            LoggerEvs.writeLog($"Listening started on port: {port}");

            TcpClient client;
            Thread    thread;

            while (true)
            {
                client = tcpListener.AcceptTcpClient();
                LoggerEvs.writeLog("New client accepted!");

                client.SendTimeout    = sendTimeout;
                client.ReceiveTimeout = receiveTimeout;

                int id = globalClientsId++;

                thread = new Thread(delegate() { requestsHandler(id); });
                Client newClient = new Client
                {
                    Id        = id,
                    TcpClient = client,
                    Thread    = thread
                };

                clients.Add(id, newClient);
                clients[id].Thread.Start();
            }
        }