예제 #1
0
 private void generateLoginMessage(ChatAppClasses.Message messageToSend)
 {
     messageToSend.MessageText = "Login Request";
     messageToSend.Type        = "Login";
     messageToSend.username    = UsernameTB.Text.ToString();
     messageToSend.password    = PasswordTB.Text.ToString();
 }
예제 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //TcpClient client = new TcpClient(Dns.GetHostName(), 1302);
                BinaryFormatter formatter = new BinaryFormatter();
                //NetworkStream networkStream = client.GetStream();

                ChatAppClasses.Message messageToSend = new ChatAppClasses.Message();
                messageToSend.MessageText = this.messageBox.Text;
                messageToSend.Type        = "Message";
                formatter.Serialize(connection.networkStream, messageToSend);
                this.messageBox.Text = "";

                //int byteCount = Encoding.ASCII.GetByteCount(messageToSend);
                //byte[] sendData = new byte[byteCount + 1];
                //sendData = Encoding.ASCII.GetBytes(messageToSend);

                //networkStream.Write(sendData, 0, sendData.Length);
                //Console.WriteLine("Sending data to the server...");

                StreamReader streamReader       = new StreamReader(connection.networkStream);
                string       responseFromServer = streamReader.ReadLine();
                Console.WriteLine(responseFromServer);

                connection.networkStream.Close();
                connection.client.Close();
            }
            catch (Exception exc)
            {
                Console.WriteLine("failed to send..." + exc.Message);
            }
        }
예제 #3
0
        private void LoginBtn_Click(object sender, EventArgs e)
        {
            if (this.LoginBtn.Text == "Login")
            {
                connection = new connectionToServer();
                string hostAddress = this.IPAddressTB.Text;
                if (hostAddress == "this" || hostAddress == "localhost")
                {
                    connection.client = new TcpClient(Dns.GetHostName(), int.Parse(this.PortTB.Text));
                }
                connection.networkStream = connection.client.GetStream();

                ChatAppClasses.Message messageToSend = new ChatAppClasses.Message();
                generateLoginMessage(messageToSend);

                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(connection.networkStream, messageToSend);

                StreamReader streamReader       = new StreamReader(connection.networkStream);
                string       responseFromServer = streamReader.ReadLine();
                this.label7.Text = responseFromServer;
                if (responseFromServer == "Logged In!")
                {
                    ActivateUserInterface_login();
                    showLoginPopup();
                }
                else if (responseFromServer == "Welcome, Admin!")
                {
                    adminApp.Show();
                    ActivateUserInterface_login();
                    ActivateAdminInterface_login();
                    showWelcomeAdminPopup();
                }
                else if (responseFromServer == "Wrong Credentials!")
                {
                    Popup popup = new Popup();
                    popup.BackColor    = Constants.WrongCredentialsPopupColor;
                    popup.message.Text = "wrong Credentials!";
                    popup.Show();
                }
                connection.networkStream.Close();
                connection.client.Close();
            }
            else if (LoginBtn.Text == "Logout")
            {
                adminApp.Visible = false;
                doLogout();
                showLogoutPopup();
            }
            else
            {
                LoginBtn.Text = "Unknown ERROR";
            }
        }
예제 #4
0
        public int saveMessageToDb(ChatAppClasses.Message message)
        {
            const string query = "INSERT INTO MESSAGES(Message, Sender, Receiver) VALUES(@Message, @Sender, @Receiver)";

            var args = new Dictionary <string, object>
            {
                { "@Message", message.MessageText },
                { "@Receiver", message.Receiver },
                { "@Sender", message.Sender }
            };

            return(ExecuteWrite(query, args));
        }
예제 #5
0
        public void run()
        {
            TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 1302);

            listener.Start();
            Console.WriteLine("Server started...");
            onlineUsers.Clear();
            while (true)
            {
                Console.WriteLine("Waiting for connection...");
                TcpClient client = acceptConnectionToClient(listener);

                NetworkStream networkStream = client.GetStream();
                StreamReader  streamReader  = new StreamReader(client.GetStream());
                StreamWriter  streamWriter  = new StreamWriter(client.GetStream());
                IFormatter    formatter     = new BinaryFormatter();

                try
                {
                    byte[] buffer = new byte[1024];
                    //networkStream.Read(buffer);
                    ChatAppClasses.Message messageFromClient = (ChatAppClasses.Message)formatter.Deserialize(networkStream);
                    //Message messageFromClient = new Message();
                    //messageFromClient.setText(Encoding.UTF8.GetString(buffer));
                    if (messageFromClient.Type == "Login")
                    {
                        Console.WriteLine(messageFromClient.username + " " + messageFromClient.password);
                    }

                    string response = handleIncomingMessage(messageFromClient);

                    networkStream.Write(Encoding.ASCII.GetBytes(response));
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed to start..." + e.Message);
                }
            }
        }
예제 #6
0
        public string handleIncomingMessage(ChatAppClasses.Message messageFromClient)
        {
            Console.WriteLine("The message from the client is: " + messageFromClient.MessageText);
            //if (messageFromClient.Type == "Login" && serverDatabase.checkCredentials(messageFromClient.username, messageFromClient.password))
            //    return "Logged In!";

            switch (messageFromClient.Type)
            {
            case "Connection":
                return("Connected");

                break;

            case "Message":
                return("Message handled!");

                break;

            case "Login":
                User user = new User();
                user.username = messageFromClient.username;
                user.password = messageFromClient.password;
                //new User() { username = messageFromClient.username, password = messageFromClient.password }
                return(handleLogin(user));

                break;

            case "SignUp":
                return(handleSignUp(new User()
                {
                    username = messageFromClient.username, password = messageFromClient.password
                }));

                break;
            }
            Server_class.serverDatabase.saveMessageToDb(messageFromClient);
            return("No Case");
        }
예제 #7
0
        private void SignUpBtn_Click(object sender, EventArgs e)
        {
            connection = new connectionToServer();
            string hostAddress = this.IPAddressTB.Text;

            if (hostAddress == "this" || hostAddress == "localhost")
            {
                connection.client = new TcpClient(Dns.GetHostName(), int.Parse(this.PortTB.Text));
            }
            connection.networkStream = connection.client.GetStream();
            BinaryFormatter formatter = new BinaryFormatter();

            ChatAppClasses.Message messageToSend = new ChatAppClasses.Message();
            messageToSend.MessageText = "Sign Up Request";
            messageToSend.Type        = "SignUp";
            messageToSend.username    = UsernameTB.Text.ToString();
            messageToSend.password    = PasswordTB.Text.ToString();

            formatter.Serialize(connection.networkStream, messageToSend);

            StreamReader streamReader       = new StreamReader(connection.networkStream);
            string       responseFromServer = streamReader.ReadLine();

            this.label7.Text = responseFromServer;
            if (responseFromServer != "Username already taken")
            {
                showSignUpPopUp();
            }
            else
            {
                showRetrySignUpPopup();
            }

            connection.networkStream.Close();
            connection.client.Close();
        }
예제 #8
0
        private void ConnectBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ConnectBtn.Text == "Connect")
                {
                    connection = new connectionToServer();
                    string hostAddress = this.IPAddressTB.Text;
                    if (hostAddress == "this" || hostAddress == "localhost")
                    {
                        connection.client = new TcpClient(Dns.GetHostName(), int.Parse(this.PortTB.Text));
                    }
                    connection.networkStream = connection.client.GetStream();

                    ConnectionLabel.Text = "Wait...";
                    ChatAppClasses.Message messageToSend = new ChatAppClasses.Message();
                    messageToSend.MessageText = "Connection Request";
                    messageToSend.Type        = "Connection";

                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(connection.networkStream, messageToSend);

                    StreamReader streamReader       = new StreamReader(connection.networkStream);
                    string       responseFromServer = streamReader.ReadLine();
                    this.ConnectionLabel.Text = responseFromServer;

                    Popup popup = new Popup();
                    popup.Show();

                    ActivateConnectionInterface();
                    this.ConnectBtn.Text      = "Disconnect";
                    this.ConnectBtn.BackColor = Constants.DisconnectBtnActive;

                    connection.client.Close();
                    connection.networkStream.Flush();
                    connection.networkStream.Close();
                }
                else if (this.ConnectBtn.Text == "Disconnect")
                {
                    this.LoginBtn.Enabled          = false;
                    LoginBtn.BackColor             = Constants.Inactive;
                    this.SignUpBtn.Enabled         = false;
                    this.SignUpBtn.BackColor       = Constants.Inactive;
                    this.ConnectionLabel.Text      = "Not Connected";
                    this.ConnectionLabel.ForeColor = Color.Red;
                    this.ConnectBtn.Enabled        = true;
                    this.label7.Text     = "Bye Bye!";
                    ConnectBtn.BackColor = Constants.ConnectBtnActive;
                    this.ConnectBtn.Text = "Connect";

                    showDisconnectPopup();
                }
                else
                {
                    this.ConnectBtn.Text = "Unknown ERROR";
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("failed to connect..." + exc.Message);
            }
        }
예제 #9
0
 public static void saveMessageToDB(ChatAppClasses.Message message)
 {
     serverDatabase.saveMessageToDb(message);
 }