Esempio n. 1
0
        static void Main(string[] args)
        {
            var server = new TcpListener(IPAddress.Loopback, 5000);

            server.Start();
            Console.WriteLine("Server started!");
            var api = new Api();

            ArrayList tempPaths = new ArrayList();

            tempPaths.Add("/api/categories");
            tempPaths.Add("/api/categories/1");
            tempPaths.Add("/api/categories/2");
            tempPaths.Add("/api/categories/3");

            ArrayList tempCategories = new ArrayList();

            tempCategories.Add(new Category {
                Id = 1, Name = "Beverages"
            });
            tempCategories.Add(new Category {
                Id = 2, Name = "Condiments"
            });
            tempCategories.Add(new Category {
                Id = 3, Name = "Confections"
            });

            api.Categories = tempCategories;
            api.Paths      = tempPaths;

            while (true)
            {
                var client = server.AcceptTcpClient();
                Console.WriteLine("Accepted client!");

                var stream = client.GetStream();

                var msg = Read(client, stream);


                if (!string.IsNullOrEmpty(msg))
                {
                    var res = new Response
                    {
                        Body   = null,
                        Status = ""
                    };

                    Request req = msg.FromJson <Request>();

                    Console.WriteLine(req.ToString());

                    CheckBadReqSystem(api, req, res, client);
                    Console.WriteLine($"Message from server {res}");
                }



                //     client.SendRequest(res.ToJson());
                Console.WriteLine($"Message from client {msg}");


                //  var data = Encoding.UTF8.GetBytes(msg.ToUpper());

                //   stream.Write(data);
            }
        }