Пример #1
0
        public async void start()
        {
            IPAddress   ip       = IPAddress.Parse("177.10.10.12");
            TcpListener listener = new TcpListener(ip, 2920);

            listener.Start();

            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();

                NetworkStream stream = client.GetStream();

                byte[] dataFromClient = new byte[1024];
                int    bytesRead      = stream.Read(dataFromClient, 0, dataFromClient.Length);
                string s = Encoding.ASCII.GetString(dataFromClient, 0, bytesRead);

                switch (s)
                {
                case "GetItems":
                {
                    content = JsonSerializer.Serialize(await itemService.getItemsAsync());
                    break;
                }

                case "GetOrders":
                {
                    content = JsonSerializer.Serialize(await orderService.getOrdersAsync());
                    break;
                }

                case "AddOrder":
                {
                    byte[] data1ToClient = Encoding.ASCII.GetBytes("Received");
                    stream.Write(data1ToClient, 0, data1ToClient.Length);
                    byte[] orderFromClient = new byte[1024];
                    int    orderRead       = stream.Read(orderFromClient, 0, orderFromClient.Length);
                    string orderString     = Encoding.ASCII.GetString(orderFromClient, 0, orderRead);
                    Console.WriteLine(orderString);
                    Order toAdd = JsonSerializer.Deserialize <Order>(orderString);
                    await orderService.addOrderAsync(toAdd);

                    content = "Order added: " + toAdd.nameOfBuyer;
                    break;
                }
                }

                byte[] dataToClient = Encoding.ASCII.GetBytes(content);
                stream.Write(dataToClient, 0, dataToClient.Length);
                client.Close();
            }
        }
Пример #2
0
        public async Task <string> getOrdersAsync()
        {
            List <Order> orders = await OrderService.getOrdersAsync();

            return(JsonSerializer.Serialize(orders));
        }