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"; } }
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(); }
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); } }