コード例 #1
0
ファイル: Server.cs プロジェクト: kidane12/RawData2017_Group8
        private static void Main(string[] args)
        {
            DataAPI.InitDataModel();
            const int port      = 5000;
            var       localAddr = IPAddress.Parse("127.0.0.1");

            var server = new TcpListener(localAddr, port);

            server.Start();

            Console.WriteLine("Started");

            while (true)
            {
                var client = server.AcceptTcpClient();

                Console.WriteLine("Client connected");

                var thread = new Thread(HandleClient);

                thread.Start(client);
            }
        }
コード例 #2
0
ファイル: Server.cs プロジェクト: kidane12/RawData2017_Group8
        private static void HandleClient(object clientObj)
        {
            if (!(clientObj is TcpClient client))
            {
                return;
            }

            var stream = client.GetStream();

            try
            {
                var requestObj = Read(stream, client.ReceiveBufferSize);

                var response = new Response();
                Console.WriteLine(requestObj.Method);

                var violatedConstraint = false;
                if (string.IsNullOrEmpty(requestObj.Date))
                {
                    response.Status   += "missing date, ";
                    violatedConstraint = true;
                }
                else if (!Helpers.IsUnixTimestamp(requestObj.Date))
                {
                    response.Status   += "illegal date, ";
                    violatedConstraint = true;
                }

                switch (requestObj.Method)
                {
                case "{}":
                    response.Status += "missing method, ";
                    break;

                case "create":
                    DataAPI.Create(requestObj, ref response, violatedConstraint);
                    break;

                case "read":
                    DataAPI.Read(requestObj, ref response, violatedConstraint);
                    break;

                case "update":
                    DataAPI.Update(requestObj, ref response, violatedConstraint);
                    break;

                case "delete":
                    DataAPI.Delete(requestObj, ref response, violatedConstraint);
                    break;

                case "echo":
                    DataAPI.Echo(requestObj, ref response, violatedConstraint);
                    break;

                default:
                    response.Status += "illegal method";
                    break;
                }

                WriteResponse(stream, response);
                stream.Close();
                client.Dispose();
            }
            catch (IOException)
            {
                Console.WriteLine("no request");
            }
        }