예제 #1
0
        private void button_Start_Click(object sender, RoutedEventArgs e)
        {
            int port;

            if (textBox_Port.Text == "" || !int.TryParse(textBox_Port.Text, out port))
            {
                MessageBox.Show("Enter a valid port number to run the server on!", "Invalid Port Number", MessageBoxButton.OK);
                return;
            }

            int numMax;

            if (!int.TryParse(textBox_Clients.Text, out numMax))
            {
                MessageBox.Show("Enter a valid number of clients to support!", "Invalid # Clients Number", MessageBoxButton.OK);
                return;
            }

            // start server
            theServer = new Server(numMax);

            try
            {
                theServer.Start(port);
            }
            catch
            {
                MessageBox.Show("Error starting server, check network connection and try again.", "Server couldn't start", MessageBoxButton.OK);
                return;
            }

            theServer.RecievedMessage    += TheServer_RecievedMessage;
            theServer.ClientConnected    += TheServer_ConnectionAccepted;
            theServer.ClientDisconnected += TheServer_ClientDisconnected;

            // display IP address and port
            string printtext = "Server started at " + theServer.GetIPAddress().ToString() + " on port " + port;

            dispText(printtext);

            // display waiting for client message
            dispText("waiting for client");

            // show # available clients
            printtext = "Available Clients = " + theServer.GetNumAvail();
            dispText(printtext);

            textBox_Clients.IsEnabled = false;
            textBox_Port.IsEnabled    = false;
            button_Start.IsEnabled    = false;
            button_Stop.IsEnabled     = true;
            textBox_Message.IsEnabled = true;
            button_Send.IsEnabled     = true;
        }