private void SendButton_Click(object sender, EventArgs e) { MethodInvoker methodInvokerDelegate = delegate() { if (this.sendMessageTextBox.Text.Length >= 1) { this.networkWatcher.Send(ProtocolCreator.Message(this.username, this.sendMessageTextBox.Text, this.sessionkey)); this.sendMessageTextBox.Text = string.Empty; if (this.sendMessageTextBox.Focused == false) { this.sendMessageTextBox.Focus(); } } }; if (this.InvokeRequired) { this.Invoke(methodInvokerDelegate); } else { methodInvokerDelegate(); } }
private void IsAliveWorker() { while (this.isReading == true) { this.Send(ProtocolCreator.IsAlive()); Thread.Sleep(2000); } }
public void EnterMessage() { Console.Clear(); Console.Write(">> "); string message = Console.ReadLine(); Protocol userMessage = ProtocolCreator.Message(this.username, message, this.sessionKey); NetworkManager.SendMessage(userMessage, outputWindowClient); }
private void CleanClosing(object sender, FormClosingEventArgs args) { DialogResult askIfLogout = MessageBox.Show("Do you want to logout?", "Chat", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (askIfLogout == DialogResult.No) { args.Cancel = true; } else if (askIfLogout == DialogResult.Yes) { this.networkWatcher.Send(ProtocolCreator.LogOut(this.username, this.sessionkey)); this.networkWatcher.Stop(); } }
public void ConnectToInputWindow() { IPEndPoint outputWindow = new IPEndPoint(IPAddress.Loopback, 90); this.inputWindowClient = new TcpClient(); NetworkManager.Connect(outputWindow, inputWindowClient); Protocol sessionData = ProtocolCreator.SessionData(this.username, this.sessionKey); NetworkManager.SendMessage(sessionData, this.inputWindowClient); this.forwardMessages = new Thread(ForwardMessagesToServer); forwardMessages.Start(); }
public ChatWindow(string username, string sessionkey, NetworkWatcher networkWatcher) { InitializeComponent(); this.FormClosing += this.CleanClosing; this.username = username; this.usernameLabel.Text = username + ":"; this.sessionkey = sessionkey; onlineUserBoxLocker = new object(); messageBoxLocker = new object(); this.networkWatcher = networkWatcher; this.networkWatcher.ConnectionLost += this.ConnectionLost; this.networkWatcher.DataReceived += this.DataReceived; this.networkWatcher.Send(ProtocolCreator.SessionKeyReceived(this.username, this.sessionkey)); }
private void LoginButton_Click(object sender, EventArgs e) { if (this.CheckIfLegalIPAddress(this.ipAddressTextBox.Text) == false) { MessageBox.Show("Please enter a legal IPv4 address!", "Chat", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); } else if (CheckIfLegalPort(this.portTextBox.Text) == false) { MessageBox.Show("The port must be between 3000 and 9000!", "Chat", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); } else if (this.usernameTextBox.Text.ToCharArray().Length < 3) { MessageBox.Show("The username must be at least 3 characters long!", "Chat", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); } else { this.networkWatcher = new NetworkWatcher(IPAddress.Parse(this.ipAddressTextBox.Text), int.Parse(this.portTextBox.Text)); this.networkWatcher.ConnectionLost += this.ConnectionLost; this.networkWatcher.DataReceived += this.DataReceived; this.networkWatcher.Start(); if (this.networkWatcher.Connected == true) { this.networkWatcher.Send(ProtocolCreator.LogIn(this.usernameTextBox.Text)); if (this.WaitForSessionKey(1000) == true) { this.networkWatcher.ConnectionLost -= this.ConnectionLost; this.networkWatcher.DataReceived -= this.DataReceived; this.Hide(); ChatWindow chatWindow = new ChatWindow(this.usernameTextBox.Text, this.sessionkey, this.networkWatcher); chatWindow.FormClosed += this.ChatWindowClosed; chatWindow.Show(); } } } }
public void ForwardMessagesToServer() { while (true) { string messageProtocol = NetworkManager.ReadMessage(inputWindowClient, 302); char[] mesageProtocolArray = messageProtocol.ToCharArray(); if (messageProtocol[0] == 'C' && messageProtocol[1] == 'H' && messageProtocol[2] == 'A' && messageProtocol[3] == 'T' && messageProtocol[4] == 'M' && messageProtocol[5] == 'E') { string messageString = string.Empty; for (int i = 6; i < mesageProtocolArray.Length; i++) { messageString = messageString + mesageProtocolArray[i]; } string[] messageProtocolContent = messageString.Split('-'); Protocol message = ProtocolCreator.Message(messageProtocolContent[0], messageProtocolContent[1], messageProtocolContent[2]); NetworkManager.SendMessage(message, serverClient); } } }
public static void DisplayLoginMenu() { Console.Clear(); ShowHeader(); Console.WriteLine(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("[!] Enter your user information below to login or press [ESC] to go back!"); Console.WriteLine(); Console.WriteLine(" " + "Username: "******" " + "Password: "******"The username must not be empty and between 3 and 10 characters!"); } Console.CursorVisible = false; Console.SetCursorPosition(23, 11); Console.CursorVisible = true; string password = GetStringWithASpecificLength(12, 23, 11, out exit, false); if (exit == false) { if (CheckIfLegalString(password, 6, 12) == false) { throw new ArgumentException("The password must not be empty and between 6 and 12 characters!"); } settings.CheckConfigFile(); IPEndPoint server = new IPEndPoint(settings.ServerIP, settings.ServerPort); TcpClient client = new TcpClient(); NetworkManager.Connect(server, client); Protocol loginRequest = ProtocolCreator.LoginRequest(username, password); NetworkManager.SendMessage(loginRequest, client); string loginResponse = NetworkManager.ReadMessage(client, 38); if (loginResponse == "CHATLI") { throw new ArgumentException("The username or the password is invalid!"); } else { char[] loginResponseArray = loginResponse.ToCharArray(); if (loginResponseArray[0] == 'C' && loginResponseArray[1] == 'H' && loginResponseArray[2] == 'A' && loginResponseArray[3] == 'T' && loginResponseArray[4] == 'L' && loginResponseArray[5] == 'O') { string sessionKey = string.Empty; for (int i = 6; i < loginResponseArray.Length; i++) { sessionKey = sessionKey + loginResponseArray[i]; } Process.Start("Client.exe", "OpenInputWindow" + " " + settings.ServerIP + " " + settings.ServerPort); OutputWindow outputWindow = new OutputWindow(username, sessionKey, client); outputWindow.Start(); } } } } } catch (Exception e) { Console.SetCursorPosition(0, 12); Console.WriteLine(); Console.WriteLine(e.Message); Console.WriteLine(); Console.WriteLine("Press any key to continue!"); Console.CursorVisible = false; Console.ReadKey(true); DisplayLoginMenu(); } Console.CursorVisible = false; Console.ResetColor(); }
public static void DisplayRegisterMenu() { Console.Clear(); ShowHeader(); Console.WriteLine(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("[!] Enter a username and a password below to register or press [ESC] to go back!"); Console.WriteLine(" If you have problems registering, press [F12] to open the help!"); Console.WriteLine(); Console.WriteLine(" " + "Username: "******" " + "Password: "******" " + "Confirm Password: "******"The username must not be empty and between 3 and 10 characters!"); } Console.CursorVisible = false; Console.SetCursorPosition(23, 12); Console.CursorVisible = true; string password = GetStringWithASpecificLength(12, 23, 12, out exit, false); if (exit == false) { if (CheckIfLegalString(password, 6, 12) == false) { throw new ArgumentException("The password must not be empty and between 6 and 12 characters!"); } Console.CursorVisible = false; Console.SetCursorPosition(31, 14); Console.CursorVisible = true; string confirmPassword = GetStringWithASpecificLength(12, 31, 14, out exit, false); if (exit == false) { if (CheckIfLegalString(confirmPassword, 6, 12) == false) { throw new ArgumentException("The confirmation password must not be empty and between 6 and 12 characters!"); } else if (password != confirmPassword) { throw new ArgumentException("The password must match the confirmation password!"); } settings.CheckConfigFile(); IPEndPoint server = new IPEndPoint(settings.ServerIP, settings.ServerPort); TcpClient client = new TcpClient(); NetworkManager.Connect(server, client); Protocol registrationRequest = ProtocolCreator.RegistrationRequest(username, password); NetworkManager.SendMessage(registrationRequest, client); string registrationResponse = NetworkManager.ReadMessage(client, 6); if (registrationResponse == "CHATRI") { throw new ArgumentException("The username already exists! Please try another one!"); } else if (registrationResponse == "CHATRO") { Console.WriteLine(); Console.WriteLine(); Console.WriteLine("The registration was successful! You can login now!"); Console.WriteLine(); Console.WriteLine("Press any key to continue!"); Console.CursorVisible = false; Console.ReadKey(true); } } } } } catch (Exception e) { Console.SetCursorPosition(0, 15); Console.WriteLine(); Console.WriteLine(e.Message); Console.WriteLine(); Console.WriteLine("Press any key to continue!"); Console.CursorVisible = false; Console.ReadKey(true); DisplayRegisterMenu(); } Console.CursorVisible = false; Console.ResetColor(); }