Exemplo n.º 1
0
        public void Service()
        {
            int       nc      = 0;
            bool      cliStop = false;
            string    MSG;
            string    thankMSG         = "Logged out";
            string    clientName       = childName;
            TcpClient clientConnection = serConnection;

            Console.Write("***Client {0,6} ", clientName);
            Console.WriteLine(" started ...");

            try
            {
                stream = clientConnection.GetStream();
                //MSG = childName + ": ";
                //MSG = MSG + "Echo service - " + "Send message or " + "just $$ to quit";
                //outBuff = Encoding.ASCII.GetBytes(MSG);
                //stream.Write(outBuff, 0, MSG.Length);
                nc = stream.Read(inBuff, 0, buffSize);

                while ((nc > 0) && (!cliStop))
                {
                    MSG = Encoding.ASCII.GetString(inBuff, 0, nc);

                    /**
                     *  STRING SEPERATORS
                     **/


                    string[] inMessage = MSG.Split(';');



                    if (MSG != EOB)
                    {
                        Console.WriteLine("Message From =>", clientName);
                        Console.WriteLine("            " + MSG);

                        string placeHolder = functionSwitch(MSG);
                        //getTutorials();

                        outBuff = Encoding.ASCII.GetBytes(placeHolder);
                        stream.Write(outBuff, 0, placeHolder.Length);



                        nc = stream.Read(inBuff, 0, buffSize);
                    }
                    else
                    {
                        //outBuff = Encoding.ASCII.GetBytes(thankMSG);

                        //stream.Write(outBuff, 0, thankMSG.Length);
                        //stream.Flush();

                        //Console.Write("***Client wants");
                        //Console.WriteLine(" to stop");

                        //cliStop = true;

                        Console.Write("***Client wants");
                        Console.WriteLine(" to stop");
                        cliStop = true;
                        outBuff = Encoding.ASCII.GetBytes(thankMSG);
                        stream.Write(outBuff, 0, thankMSG.Length);//break point
                        stream.Flush();
                    }
                }
                stream.Close();
                clientConnection.Close();
                Console.Write("***Client {0,6} ", clientName);
                Console.WriteLine("stopped!");
            }
            catch (Exception ex)
            {
                Console.Write("Err: Client {0,6} left ", clientName);
                Console.WriteLine(" or " + ex.Message);
            }
        }