Ejemplo n.º 1
0
        public void DoClient(TcpClient socket)
        {
            using (socket)
            {
                NetworkStream ns = socket.GetStream();
                StreamReader  sr = new StreamReader(ns);
                StreamWriter  sw = new StreamWriter(ns);
                sw.AutoFlush = true;

                string line;
                while (true)
                {
                    line = sr.ReadLine();

                    Console.WriteLine("Client:" + line);

                    string[] stringSplit = line.Split(' ');

                    if (stringSplit[0] == "GetAll")
                    {
                        string bog = JsonConvert.SerializeObject(_bogliste);

                        Console.WriteLine(socket.Client.RemoteEndPoint + ": " + bog);

                        sw.WriteLine(bog);
                    }

                    if (stringSplit[0] == "GetByISBN13")
                    {
                        if (stringSplit.Length == 2)
                        {
                            Bog bogen = _bogliste.Find(bog => bog.Isbn13 == stringSplit[1]);
                            sw.WriteLine(JsonConvert.SerializeObject(bogen));
                        }
                    }

                    if (stringSplit[0] == "Save")
                    {
                        if (stringSplit.Length >= 2)
                        {
                            Bog nyBog = JsonConvert.DeserializeObject <Bog>(stringSplit[1]);
                            sw.WriteLine("Tilføjer ny bog ved navn:" + nyBog.Titel);
                            _bogliste.Add(nyBog);
                        }
                    }
                }
            }
        }
Ejemplo n.º 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;

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

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

                switch (command)
                {
                case "GetAll":
                    sw.WriteLine(JsonConvert.SerializeObject(books));
                    break;

                case "Get":
                    sw.WriteLine(JsonConvert.SerializeObject(books.Find(id => id.Isbn13 == param)));
                    break;

                case "Save":
                    Bog saveBog = JsonConvert.DeserializeObject <Bog>(param);
                    books.Add(saveBog);
                    break;
                }
                line = sr.ReadLine();
            }
            ns.Close();
            socket.Close();
        }
Ejemplo n.º 3
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 bog");
                    Bog gemBog = JsonConvert.DeserializeObject <Bog>(param);
                    bogs.Add(gemBog);
                    break;

                // kigger hvis en søgning er forkert

                default:
                    sw.WriteLine("Søgning gav ingen resultat");
                    break;
                }

                sr.ReadLine();
            }

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