Beispiel #1
0
        private static void ThreadProc(object obj)
        {
            try
            {
                TcpClient client = (TcpClient)obj;

                Console.WriteLine("New connection!");

                byte[]        bytes  = new byte[512];
                NetworkStream stream = client.GetStream();

                int i;

                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    string data = Encoding.ASCII.GetString(bytes, 0, i);

                    Console.WriteLine("Received:\n{0}", data);

                    byte[] msg = null;

                    var split = data.Split(' ');

                    if (split[0] == "ElBil")
                    {
                        int price = int.Parse(split[1]);

                        Afgift afgift = new Afgift();
                        data = Afgift.elBilAfgift(price).ToString();
                    }
                    else if (split[0] == "Bil")
                    {
                        int price = int.Parse(split[1]);

                        Afgift afgift = new Afgift();
                        data = Afgift.bilAfgift(price).ToString();
                    }
                    else
                    {
                        data = "Incorrect format.";
                    }

                    msg = Encoding.ASCII.GetBytes(data);

                    stream.Write(msg, 0, msg.Length);
                    Console.WriteLine("Sent:\n{0}", data);
                    break;
                }

                client.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }