示例#1
0
        public static void DoClient(TcpClient socket)
        {
            Stream       ns = socket.GetStream();
            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);

            sw.AutoFlush = true;

            string besked = sr.ReadLine();
            string answer = "";


            //if ()
            while (besked != null && besked != "")
            {
                string[] messageArray = besked.Split(' ');
                string   param        = besked.Substring(besked.IndexOf(' ') + 1);
                string   command      = messageArray[0];

                switch (command)
                {
                case "GetAll":
                    sw.WriteLine("Get all udført");
                    sw.WriteLine(JsonConvert.SerializeObject(bøger));
                    break;


                case "Get":
                    sw.WriteLine(messageArray[1]);
                    sw.WriteLine(JsonConvert.SerializeObject(bøger.Find(id => id.Isbn13 == param)));
                    break;


                case "Save":
                    sw.WriteLine("Save modtaget");
                    Bog saveBook = JsonConvert.DeserializeObject <Bog>(param);
                    bøger.Add(saveBook);
                    break;


                default:
                    sw.WriteLine("Fejlsøgning");
                    break;
                }

                besked = sr.ReadLine();
            }


            ns.Close();
            socket.Close();
        }
示例#2
0
        public static void DoClient(TcpClient socket)
        {
            NetworkStream ns = socket.GetStream();

            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);

            sw.AutoFlush = true;

            Bog returnerBog = JsonConvert.DeserializeObject <Bog>(sr.ReadLine());

            string line   = sr.ReadLine();
            string answer = "";

            while (line != null && line != "")
            {
                Console.WriteLine("Bog information" + "" + line);
                answer = line.ToUpper();
                sw.WriteLine(answer);
                line        = sr.ReadLine();
                returnerBog = JsonConvert.DeserializeObject <Bog>(sr.ReadLine());
                line        = returnerBog.ToString();

                string[] lineArray = line.Split("");
                string   param     = line.Substring(line.IndexOf("") + 1);
                string   command   = lineArray[0];

                switch (command)
                {
                // henter alle bøger
                case "GetAll":
                    sw.WriteLine("Hent alle bøger");
                    sw.WriteLine(JsonConvert.SerializeObject(bogs));
                    break;

                // Henter isbn nummeret
                case "Get":
                    sw.WriteLine("Hent min bog og isbn" + lineArray[1] + bogs);
                    sw.WriteLine(JsonConvert.SerializeObject(bogs.Find(bog => bog.Isbn13 == param)));
                    break;

                // gemmer bog

                case "Save":
                    sw.WriteLine("Gem en bog");
                    Bog gemBog = JsonConvert.DeserializeObject <Bog>(param);
                    bogs.Add(gemBog);
                    break;

                // kigger hvis en søgning er forkert

                default:
                    sw.WriteLine("Du har søgt på en forkert bog");
                    break;
                }

                sr.ReadLine();
            }

            ns.Close();
            socket.Close();
        }