Exemple #1
0
        //
        // METHOD		:	MainWindow_Close()
        // DESCRIPTION	:	This method shuts down all the running functions and threads when the
        //                  user exits the program
        // PARAMETERS   :   object sender       :   object that called the funtion
        //                  CancelEventArgs e   :   any arguments if an event is canceled
        // RETURNS		:	void
        //
        private void Close_Window(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // try catch to catch and exceptions thrown
            try
            {
                Byte[] data = System.Text.Encoding.ASCII.GetBytes("--A Client Has Disconnected--");

                stream.Write(data, 0, data.Length);

                // stop the client from listening to server
                keepListening = false;

                // close Stream and client
                stream.Close();
                client.Close();
            }
            catch (SocketException Se)
            {
                MessageBox.Show(Se.ToString());
            }
            catch (ArgumentNullException ARGe)
            {
                MessageBox.Show(ARGe.ToString());
            }
        }
Exemple #2
0
        //	FUNCTION    : Send_Button_Click
        //	DESCRIPTION : When the send button is clicked if there is text in the text box
        //                translate the message and submit it to the chat
        //	PARAMETERS  : Object : o
        //	RETURNS     : NONE
        private void Send_Button_Click(object sender, RoutedEventArgs e)
        {
            if ((messageText.Text.Length != 0) && (messageText.Text != " "))
            {
                string recievedMessage = messageText.Text;

                // try catch to catch any exceptions thrown
                try
                {
                    // translate the message into ASCII and then store into array
                    Byte[] data = System.Text.Encoding.ASCII.GetBytes(recievedMessage);

                    // if stream isn't null send the message to the server
                    if (stream == null)
                    {
                        MessageBox.Show("Error: Server Offline");
                    }
                    else
                    {
                        stream.Write(data, 0, data.Length);
                    }

                    // submit  the message to the chatBox
                    SubmitToChat(recievedMessage, "You");
                }
                catch (SocketException Se)
                {
                    MessageBox.Show(Se.ToString());
                }
                catch (ArgumentNullException ARGe)
                {
                    MessageBox.Show(ARGe.ToString());
                }

                // reset message box text
                messageText.Text = "";
            }
        }