private void clientSend(string text)
 {
     if (client.isConnected())
     {
         client.sendCommand(text, client.CLIENT.GetStream());
     }
 }
Example #2
0
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (client != null)
            {
                client.sendCommand("++CLOSE", client.CLIENT.GetStream());
                client.close();
            }

            base.OnClosing(e);
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (client != null)
            {
                try
                {
                    client.sendCommand("++CLOSE", client.CLIENT.GetStream());
                    _isRunning = false;
                }
                catch (Exception e2)
                {
                    Console.WriteLine(e2.Message);
                    _isRunning = false;
                    // MessageBox.Show("Errore di rete, Server offline");
                    //this.Hide();
                    this.Hide();
                }
            }

            base.OnClosing(e);
        }
        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            string username = this.usernameTxtBox.Text;
            string password = this.passwordTxtBox.Password;

            errorBox_Username.Visibility = System.Windows.Visibility.Hidden;
            errorBox_Password.Visibility = System.Windows.Visibility.Hidden;

            /////////////////////////////////////////////////////////////////////
            if (username.Equals(""))
            {
                errorBox_Username.Visibility = System.Windows.Visibility.Visible;
                Errore = true;
            }


            if (password.Equals(""))
            {
                errorBox_Password.Visibility = System.Windows.Visibility.Visible;
                Errore = true;
            }


            if (Errore)
            {
                return;
            }
            ////////////////////////////////////////////////////////////////

            TcpClient tmp = new TcpClient();

            // ip e porta del server fissati
            tmp.Connect(classes.Function.checkIPAddress("172.29.146.235"), Int16.Parse("3000"));
            client = new Network.Client(tmp);
            string ccc = client.reciveComand(client.CLIENT.GetStream());

            if (ccc.Equals("+++OPEN"))
            {
                client.sendCommand("++++REG", client.CLIENT.GetStream());
                client.sendFileDimension(username.Length, client.CLIENT.GetStream());

                ccc = client.reciveComand(client.CLIENT.GetStream());
                if (ccc.Equals("+++++OK"))
                {
                    client.sendData(username, client.CLIENT.GetStream());
                }

                ccc = client.reciveComand(client.CLIENT.GetStream());
                if (ccc.Equals("+++++OK"))
                {
                    client.sendFileDimension(password.Length, client.CLIENT.GetStream());
                }

                ccc = client.reciveComand(client.CLIENT.GetStream());
                if (ccc.Equals("+++++OK"))
                {
                    client.sendData(password, client.CLIENT.GetStream());
                }
            }


            errorBox.Content    = "Registration succeed";
            errorBox.Visibility = System.Windows.Visibility.Visible;
            errorBox.Foreground = Brushes.Green;

            System.Threading.Thread.Sleep(1000);
            this.Hide();
            this.parent.Activate();
            this.parent.Show();
        }
Example #5
0
        public string login()
        {
            string cmd;

            Network.Client myClient = client;
            NetworkStream  ns       = myClient.CLIENT.GetStream();

            errorBox_Username.Visibility = System.Windows.Visibility.Hidden;
            errorBox_Password.Visibility = System.Windows.Visibility.Hidden;
            cmd = myClient.reciveComand(ns);

            if (cmd.Equals("+++OPEN"))
            {
                Connect.Dispatcher.Invoke(new Action(() =>
                {
                    username = usernameTxtBox.Text;
                    password = paswordTxtBox.Password;
                }), DispatcherPriority.ContextIdle);


                if (username.Equals(""))
                {
                    errorBox_Username.Visibility = System.Windows.Visibility.Visible;
                }


                if (password.Equals(""))
                {
                    errorBox_Password.Visibility = System.Windows.Visibility.Visible;
                }

                myClient.sendCommand("++LOGIN", ns);
                cmd = myClient.reciveComand(ns);
                if (cmd.Equals("+++++OK"))
                {
                    myClient.sendFileDimension(username.Length + password.Length + 1, ns);
                    cmd = myClient.reciveComand(ns);
                    if (cmd.Equals("+++++OK"))
                    {
                        string user = username + ":" + password;

                        myClient.sendData(user, ns);
                        string tmp = myClient.reciveComand(ns);

                        if (tmp.Equals("++CLOSE"))
                        {
                            myClient.close();
                            return(username);
                        }
                        else
                        {
                            errorBox.Visibility = System.Windows.Visibility.Visible;
                            myClient.close();
                            return(null);
                        }
                    }
                }
            }

            return(null);
        }