コード例 #1
0
ファイル: Program.cs プロジェクト: babujyan/assignment12
        static double MathOperetions(string data)
        {
            var oper      = data.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0];
            var firstArg  = data.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[1];
            var secondArg = data.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[2];


            Double.TryParse(firstArg, out double first);
            Double.TryParse(secondArg, out double second);

            MathServer mathServer = new MathServer();

            switch (oper)
            {
            case "+":
                return(mathServer.Add(first, second));

            case "-":
                return(mathServer.Sub(first, second));

            case "*":
                return(mathServer.Mult(first, second));

            case "/":
                return(mathServer.Div(first, second));

            default:
                throw new Exception("no such operator listed.");
            }
        }
コード例 #2
0
        private void Start(Socket clientSocket)
        {
            while (true)
            {
                byte[] buffer = new byte[clientSocket.SendBufferSize];

                int    bytes = clientSocket.Receive(buffer);
                string msg   = (Encoding.UTF8.GetString(buffer, 0, bytes));


                if (msg.Equals("close"))
                {
                    clientSocket.Close();
                    return;
                }
                var        args        = msg.Split(':');
                MathServer mathServer  = new MathServer();
                double     firsValue   = Convert.ToDouble(args[1]);
                double     secondValue = Convert.ToDouble(args[2]);
                double     result      = 0;

                switch (args[0])
                {
                case "+":
                    result = mathServer.Add(firsValue, secondValue);
                    break;

                case "-":
                    result = mathServer.Sub(firsValue, secondValue);
                    break;

                case "/":
                    result = mathServer.Div(firsValue, secondValue);
                    break;

                case "*":
                    result = mathServer.Mult(firsValue, secondValue);
                    break;

                default:
                    Console.WriteLine("you write Wrong operation!!");
                    return;
                }

                buffer = Encoding.UTF8.GetBytes(result.ToString());
                clientSocket.Send(buffer);
            }
        }
コード例 #3
0
        private void Start(byte[] buffer)
        {
            string msg  = (Encoding.UTF8.GetString(buffer, 0, buffer.Length));
            var    args = msg.Split(':');

            MathServer mathServer  = new MathServer();
            double     firsValue   = Convert.ToDouble(args[1]);
            double     secondValue = Convert.ToDouble(args[2]);
            double     result      = 0;

            switch (args[0])
            {
            case "+":
                result = mathServer.Add(firsValue, secondValue);
                break;

            case "-":
                result = mathServer.Sub(firsValue, secondValue);
                break;

            case "/":
                result = mathServer.Div(firsValue, secondValue);
                break;

            case "*":
                result = mathServer.Mult(firsValue, secondValue);
                break;

            default:
                Console.WriteLine("you write Wrong operation!!");
                return;
            }

            buffer = Encoding.UTF8.GetBytes(result.ToString());
            listener.Send(buffer, buffer.Length, groupEP);
        }