예제 #1
0
 private void openChatBoxPanel_Click_1(object sender, EventArgs e)
 {
     openChatAlertPanel.Visible = false;
     openChatBoxPanel.Visible   = false;
     writeAllMessages();
     chatPanel.Visible = true;
     chatPanel.BringToFront();
 }
예제 #2
0
        // 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");
            }
        }
예제 #3
0
        // 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.");
            }
        }