private void New(object sender, RoutedEventArgs e) { SetBooleans(true); DataGrid.UnselectAll(); TypeBox.Focus(); }
// Method: StartServer_Click() // Description: This method takes the port from the interface, validates it // and once validated, it fire ups the server and send user to // chat screen. private void StartServer_Click(object sender, EventArgs e) { // application is running server, update the variable to let other methods know ServerItIs = true; // get port from interface and clear the textbox string portString = ServerPort.Text; ServerPort.Clear(); bool valid = false; // validate port int temp = 0; int.TryParse(portString, out temp); if (temp > 0) { valid = true; } // start server if port is valid if (valid) { Int32 port = Int32.Parse(portString); IPAddress ip = IPAddress.Parse("127.0.0.1"); try { server = new TcpListener(ip, port); // start server server.Start(); // bring chat panel to front ChatPanel.BringToFront(); TypeBox.Focus(); // update textbox at the top with IP address and port ConnectionInfo.Text = "IP: " + GetLocalIPAddress() + " Port: " + port; // start thread to handle the client ThreadPool.QueueUserWorkItem(HandleClient); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else { MessageBox.Show("Port cannot be alphabet, special character or negative number." + "Please try again"); } }
private void HandleThreadClick(object sender, MouseButtonEventArgs e) { var ctrl = sender as ThreadControl; if (sender is ThreadControl) { _currentThread = ctrl.ThreadId; ProcessThreadId(ctrl.ThreadId, true); TypeBox.IsReadOnly = false; TypeBox.Focus(); } }
//Setup Game Setting private void InitializeGame(int score) { game = new Game(score); //Game Setup Dead.Text = ""; //Health Bar TypeBox.Text = ""; //Type Area TimeCounter.Text = ""; //Reset Timer for Time Left words = new List <TextBlock>(Game.NoOfBlock); //Reset the number of words in game for (int i = 0; i < Game.Lives; i++) { Lives[i] = LifeGrid.Children[i] as Image; //Health Bar Images Lives[i].Visibility = Visibility.Visible; } Start start = new Start(Start_Game) + new Start(Start_Time) + new Start(Background_Timer); //Chain up all timer into delegate Start() start(); TypeBox.Focus(); //Cursor on Typebox }
// Method: StartClient_Click() // Description: This method takes the IP and port from the interface, validates it // and once validated, it tries to connect to server, once connected, // send user to chat screen. private void StartClient_Click(object sender, EventArgs e) { bool valid = false; IPAddress ip; int port = 0; // validate ip and port if (IPAddress.TryParse(ipbox.Text, out ip) && int.TryParse(clientport.Text, out port)) { if (port > 0) { valid = true; } } // initialise client and connect to server if valid if (valid) { RunTheClient = new TcpClient(); try { // connect to server RunTheClient.Connect(ip, port); if (RunTheClient.Connected) { ChatPanel.BringToFront(); ConnectionInfo.Text = "Connected to server " + ip.ToString() + " at port " + port; TypeBox.Focus(); // start thread to receive messages from server ThreadPool.QueueUserWorkItem(HandleServer); } } // connection refused catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else { MessageBox.Show("Invalid entry. Please try again."); } }
// Method: SendEncrypted_Click() // Description: This method takes the message from textbox, and send it over on // the established TCP stream encrypted with Bruce Scheneir's algorithm. private void SendEncrypted_Click(object sender, EventArgs e) { // get message from textbox to be sent string msg = TypeBox.Text; if (msg == "") { return; } TypeBox.Clear(); StreamWriter sw; string key = "#981#-"; // encrypt it and add the key to let the other server/client know that it is // encrypyed string encrypted = key + b.Encrypt_CBC(msg); try { // establish writer to write to stream if (ServerItIs) { sw = new StreamWriter(client.GetStream()); } else { sw = new StreamWriter(RunTheClient.GetStream()); } sw.AutoFlush = true; // send encrypted message sw.WriteLine(encrypted); // add to chat history ChatHistory.Items.Add("Me: " + msg); TypeBox.Focus(); ScrollToBottom(); } catch (Exception) { MessageBox.Show("No clients have connected yet... Try again later!", "Error 643"); } }
// Method: SendNormal_Click() // Description: This method takes the message from textbox, and send it over on // the established TCP stream (unencrypted). private void SendNormal_Click(object sender, EventArgs e) { // get message from interface and make sure it is not empty string msg = TypeBox.Text; if (msg == "") { return; } // clear typebox after reading message TypeBox.Clear(); StreamWriter sw; try { // start the stream based on whether or not it is server if (ServerItIs) { sw = new StreamWriter(client.GetStream()); } else { sw = new StreamWriter(RunTheClient.GetStream()); } sw.AutoFlush = true; // write message to stream sw.WriteLine(msg); // add the chat history ChatHistory.Items.Add("Me: " + msg); TypeBox.Focus(); ScrollToBottom(); } catch (Exception) { MessageBox.Show("No clients have connected yet... Try again later!", "Error 643"); } }
private void Update(object sender, RoutedEventArgs e) { SetBooleans(true); TypeBox.Focus(); }