Exemplo n.º 1
0
        public void Start()
        {
            //start the server
            IPAddress iPAddress = Dns.GetHostEntry(hOST).AddressList[0];

            TcpListener = new TcpListener(iPAddress, pORT);


            try
            {
                TcpListener.Start();
                Console.WriteLine("Server started...");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            while (true)
            {
                TcpClient tcpClient = TcpListener.AcceptTcpClient();
                //handle login

                Message message = new Message();
                message.ReadFrom(tcpClient.GetStream());
                string username = message.Body.Split(';')[0];
                Console.WriteLine("username : "******"password : "******"Client authenticated is connected...");
                    RegisterClient(username, tcpClient);
                    Message response = new Message();

                    response.Body = username;
                    handlers["IS_AUTHENTICATED"].Invoke(response);
                    Thread thread = new Thread(() => HandleRequest(tcpClient));
                    thread.Start();
                }
                else
                {
                    Message response = new Message();
                    response.Header = "404";
                    response.Body   = "Username of password is not valid";
                    handlers["AUTHENTICATION_FAILED"].Invoke(response);
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            const string HOST = "localhost";
            const int    PORT = 8080;

            LoginServiceServer loginService = new LoginServiceServer();
            TCPServer          tCPServer    = new TCPServer(HOST, PORT);


            tCPServer.addHandler("CHAT", delegate(Message request)
            {
                //save message into db

                return(null);
            });

            tCPServer.addHandler("CHECK_LOGIN", delegate(Message request)
            {
                string username = request.Body.Split('?')[0];
                Console.WriteLine("username : "******"password : "******"OK";
                    response.Body   = username;
                }
                else
                {
                    response.Header = "404";
                    response.Body   = "Username or password is invalid";
                }

                return(response);
            });

            tCPServer.Start();
        }