Exemple #1
0
        private async void HostButton_Click(object sender, EventArgs e)
        {
            TcpListener server = null;

            HostButton.Enabled = false;
            JoinButton.Enabled = false;
            try
            {
                Int32     port      = Convert.ToInt32(PortTextBox.Text);
                IPAddress localAddr = IPAddress.Parse(IPTextBox.Text);

                server = new TcpListener(localAddr, port);
                server.Start();

                InfoLabel.Text = WAIT;

                TcpClient client = await server.AcceptTcpClientAsync();

                server.Stop();
                InfoLabel.Text = CONNECTED;

                // Get a stream object for reading and writing
                stream = client.GetStream();

                WriteMessage(CONNECTED);
                String response = ReadMessage();
                InfoLabel.Text = response.ToString();

                ChessUI chess = new ChessUI(stream, figureColor.White);
                chess.ShowDialog();
                InfoLabel.Text = END;
                SendDisconnectMessage();
                client.Close();
            }
            catch (SocketException)
            {
                InfoLabel.Text = HOSTING_ERROR;
            }
            catch (FormatException)
            {
                InfoLabel.Text = BAD_IP_PORT;
            }
            finally
            {
                // Stop listening for new clients
                if (server != null)
                {
                    server.Stop();
                }
                HostButton.Enabled = true;
                JoinButton.Enabled = true;
            }
        }
Exemple #2
0
        private void JoinButton_Click(object sender, EventArgs e)
        {
            try{
                // Create a TcpClient
                string    ip     = IPTextBox.Text;
                Int32     port   = Convert.ToInt32(PortTextBox.Text);
                TcpClient client = new TcpClient(ip, port);

                InfoLabel.Text = "Connecting to host...";

                // Get a client stream for reading and writing.
                this.stream = client.GetStream();

                WriteMessage("1.0");

                String data = ReadMessage();

                if (data == "Connected")
                {
                    InfoLabel.Text = "Connected!";
                    this.Hide();
                    ChessUI chess = new ChessUI(stream, "White");
                    chess.ShowDialog();
                }
                else
                {
                    if (data == "Invalid version")
                    {
                        InfoLabel.Text = "Wrong version!";
                    }
                    else
                    {
                        InfoLabel.Text = data;
                    }
                    stream.Close();
                }

                this.Show();

                SendDisconnectMessage();

                // Close everything
                client.Close();
            }
            catch (ArgumentNullException err) {
                InfoLabel.Text = "ArgumentNullException: " + err;
            }
            catch (SocketException err) {
                err.ToString();
                InfoLabel.Text = "Could not connect to host";
            }
        }
Exemple #3
0
        private void HostButton_Click(object sender, EventArgs e)
        {
            TcpListener server = null;

            try{
                Int32     port      = Convert.ToInt32(PortTextBox.Text);
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");

                server = new TcpListener(localAddr, port);
                server.Start();

                InfoLabel.Text = "Waiting for connection...";

                TcpClient client = server.AcceptTcpClient();
                InfoLabel.Text = "Connected!";

                // Get a stream object for reading and writing
                this.stream = client.GetStream();

                String data = ReadMessage();

                if (data == this.version)
                {
                    WriteMessage("Connected");

                    this.Hide();
                    ChessUI chess = new ChessUI(stream, "Black");
                    chess.ShowDialog();

                    SendDisconnectMessage();
                }
                else
                {
                    WriteMessage("Invalid version");
                }

                this.Show();
            }
            catch (SocketException err) {
                InfoLabel.Text = "SocketException: " + err;
            }
            finally{
                // Stop listening for new clients
                server.Stop();
            }
        }
Exemple #4
0
        private async void JoinButton_Click(object sender, EventArgs e)
        {
            bool res = false;

            HostButton.Enabled = false;      //
            JoinButton.Enabled = false;      //
            InfoLabel.Text     = CONNECTING; //
            await Task.Run(() => res = Join());

            if (!res)
            {
                InfoLabel.Text = CONNECTING_ERROR;
            }
            else
            {
                String data = ReadMessage();
                InfoLabel.Text = data.ToString();
                WriteMessage(CONNECTED);

                ChessUI chess = new ChessUI(stream, figureColor.Black);
                chess.ShowDialog();
                InfoLabel.Text = END;
                SendDisconnectMessage();

                // Close everything
                client.Close();
                stream.Close();
            }
            HostButton.Enabled = true;
            JoinButton.Enabled = true;

            /*try
             * {
             *
             *  // Create a TcpClient
             *  string ip = IPTextBox.Text;
             *  Int32 port = Convert.ToInt32(PortTextBox.Text);
             *  TcpClient client = new TcpClient(ip, port);
             *
             *
             *  // Get a client stream for reading and writing.
             *  this.stream = client.GetStream();
             *
             *
             *  String data = ReadMessage();
             *  InfoLabel.Text = data.ToString();
             *  WriteMessage("Connected");
             *
             *  ChessUI chess = new ChessUI(stream, figureColor.Black);
             *  chess.ShowDialog();
             *  InfoLabel.Text = "end";
             *  SendDisconnectMessage();
             *
             *  // Close everything
             *  client.Close();
             *  stream.Close();
             *  HostButton.Enabled = true;
             *  JoinButton.Enabled = true;
             * }
             * catch (SocketException)
             * {
             *  InfoLabel.Text = "Could not connect to host";
             *  HostButton.Enabled = true;
             *  JoinButton.Enabled = true;
             * }*/
        }