예제 #1
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");
            }
        }