コード例 #1
0
        private async void LoginBtn_Click(object sender, EventArgs e)
        {
            try
            {
                int port = int.Parse(this.PortTextBox.Text);
                if (port < 0)
                {
                    throw new ArgumentException("'port' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(this.IPTextBox.Text))
                {
                    throw new ArgumentException("'ip' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(this.LoginTextBox.Text))
                {
                    throw new ArgumentException("'login' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(this.PasswordTextBox.Text))
                {
                    throw new ArgumentException("'pasword' is incorrect");
                }
                string address  = this.IPTextBox.Text;
                string login    = this.LoginTextBox.Text;
                string password = this.PasswordTextBox.Text;

                Program.client = new TcpClient(address, port);
                Program.stream = Program.client.GetStream();


                string request = tcpService.SerializeAuthorizeRequest(login, password);
                byte[] data    = tcpService.CodeStream(request);
                Program.stream.Write(data, 0, data.Length);
                string response = tcpService.DecodeStream(Program.stream);
                User   user     = tcpService.DeserializeAuthorizeResponse(response);
                if (user.Login == null || user.Password == null || !user.Login.Equals(login) || !user.Password.Equals(password))
                {
                    throw new ArgumentException("login or password is incorrect");
                }
                Program.user = user;
                Program.ip   = address;
                Program.port = port;
                Form mainForm = new MainForm();
                mainForm.Left = this.Left;
                mainForm.Top  = this.Top;
                mainForm.Show();
                this.Hide();
            }
            catch (Exception ex) {
                this.StatusLabel.Text = "Status: " + ex.Message;
                if (Program.client != null)
                {
                    Program.client.Close();
                }
                if (Program.stream != null)
                {
                    Program.stream.Close();
                }
            }
        }
コード例 #2
0
        private async void LoginBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LoginBtn.IsEnabled = false;
                int port = int.Parse(this.PortTextBox.Text);
                if (port < 0)
                {
                    throw new ArgumentException("'port' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(this.IPTextBox.Text))
                {
                    throw new ArgumentException("'ip' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(this.LoginTextBox.Text))
                {
                    throw new ArgumentException("'login' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(this.PasswordBox.Password))
                {
                    throw new ArgumentException("'pasword' is incorrect");
                }
                string address  = this.IPTextBox.Text;
                string login    = this.LoginTextBox.Text;
                string password = this.PasswordBox.Password;

                SingletoneObj.Client = new TcpClient(address, port);
                SingletoneObj.Stream = SingletoneObj.Client.GetStream();


                string request = tcpService.SerializeAuthorizeRequest(login, password);
                byte[] data    = await tcpService.CodeStreamAsync(request);

                await SingletoneObj.Stream.WriteAsync(data, 0, data.Length);

                string response = await tcpService.DecodeStreamAsync(SingletoneObj.Stream);

                User user = tcpService.DeserializeAuthorizeResponse(response);
                if (user.Login == null || user.Password == null || !user.Login.Equals(login) || !user.Password.Equals(password))
                {
                    throw new ArgumentException("login or password is incorrect");
                }
                SingletoneObj.User = user;
                SingletoneObj.IP   = address;
                SingletoneObj.Port = port;
                SingletoneObj.Windows.Add("AuthorizationForm", this);
                MainForm form = new MainForm();
                form.Left = (this.Width - form.Width) + this.Left;
                form.Top  = (this.Height - form.Height) + this.Top;
                form.Show();
            }
            catch (Exception ex)
            {
                LoginBtn.IsEnabled       = true;
                this.StatusLabel.Content = "Status: " + ex.Message;
                if (SingletoneObj.Client != null)
                {
                    SingletoneObj.Client.Close();
                }
                if (SingletoneObj.Stream != null)
                {
                    SingletoneObj.Stream.Close();
                }
            }
        }