Ejemplo n.º 1
0
        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            if (txtLogin.Text.Count() == 0)
            {
                MessageBox.Show("Enter login!");
                return;
            }

            TcpClient newClient = new TcpClient();

            try
            {
                newClient.Connect(new IPAddress(new byte[] { 127, 0, 0, 1 }), 12345);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error");
                newClient.Close();
                this.DialogResult = false;
                this.Close();
                return;
            }
            var bw = new BinaryWriter(newClient.GetStream());

            bw.Write((byte)Command.CheckLogin);

            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(newClient.GetStream(), txtLogin.Text);
            bool result = Convert.ToBoolean(bf.Deserialize(newClient.GetStream()));

            if (result)
            {
                mainWindow.SetTcpClient(newClient);
                mainWindow.SetLogin(txtLogin.Text);
                mainWindow.Title  = "Chat Client: " + txtLogin.Text;
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                bw.Close();
                newClient.Close();
                txtMessage.Opacity = 1;
                DoubleAnimation da = new DoubleAnimation(1, 0, new TimeSpan(0, 0, 0, 3));
                txtMessage.BeginAnimation(TextBlock.OpacityProperty, da);
            }
        }