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();
        }
        public FolderBackup(string cnt)
        {
            this.auth     = cnt;
            lstFilesFound = new List <string>();
            files         = new Dictionary <string, Utils.FileInfomation>();
            info          = new List <Utils.FileInfomation>();
            backupList    = new List <Utils.CheckBackup>();

            InitializeComponent();

            if (!Directory.Exists(backupFolder))
            {
                Directory.CreateDirectory(backupFolder);
            }

            try
            {
                // ip e porta del server fissati

                TcpClient tmp = new TcpClient();
                //TcpClient control = new TcpClient();
                _isRunning = true;
                // ip e porta del server fissati
                tmp.Connect(classes.Function.checkIPAddress("192.168.137.111"), Int16.Parse("3000"));
                //tmp.Connect(classes.Function.checkIPAddress("127.0.0.1"), Int16.Parse("3000"));
                client = new Network.Client(tmp);

                string ccc = client.reciveComand(client.CLIENT.GetStream());
                if (ccc.Equals("+++OPEN"))
                {
                    Thread th = new Thread(CheckCon);
                    th.Start(client.CLIENT);
                    clientSend("+++AUTH");
                    string cmd = client.reciveComand(client.CLIENT.GetStream());
                    Console.WriteLine(cmd);
                    if (cmd.Equals("+++++OK"))
                    {
                        client.sendFileDimension(this.auth.Length, client.CLIENT.GetStream());
                        cmd = client.reciveComand(client.CLIENT.GetStream());
                        if (cmd.Equals("+++++OK"))
                        {
                            Console.WriteLine(cmd);
                            client.sendData(this.auth, client.CLIENT.GetStream());
                        }
                    }

                    clientSend(String.Format("+++LIST"));

                    dir.Dispatcher.Invoke(new Action(() =>
                    {
                        dir.Text += "Client connected to server!\n";
                    }), DispatcherPriority.ContextIdle);

                    clientRecive();
                }
            }
            catch (SocketException e1)
            {
                MessageBox.Show("Server non ragiungibile!");
                this.client = null;
                return;
            }
            catch (FormatException e2)
            {
                MessageBox.Show("Porta errata!");
                this.client = null;
                return;
            }
            catch (InvalidOperationException e3)
            {
                MessageBox.Show("Indirizzo IP errato!");
                this.client = null;
                return;
            }
            catch (Exception e)
            {
                MessageBox.Show("Connessione col server persa!");
                return;
            }
        }
Example #3
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);
        }
        private void clientRecive()
        {
            if (client.isConnected())
            {
                new Thread(() =>
                {
                    string cmd;
                    NetworkStream ns = client.CLIENT.GetStream();
                    // Send the message to the connected TcpServer.


                    while (_isRunning && !Shutdown)
                    {
                        try
                        {
                            cmd = client.reciveComand(ns);
                            Console.WriteLine(cmd);
                            if (cmd == null)
                            {
                                _isRunning = false;
                                return;
                            }
                            if (cmd.Equals("++CLOSE"))
                            {
                                client.close();
                                _isRunning = false;
                                Application.Current.Shutdown();
                                return;
                            }
                            if (cmd == null)
                            {
                                _isRunning = false; return;
                            }
                            if (cmd.Equals("+++LIST"))
                            {
                                int dim = client.reciveDimension(ns);
                                Console.WriteLine(dim);
                                clientSend("+++++OK");
                                String version = client.reciveVersion(dim, ns);
                                Console.WriteLine(version);
                                String[] list = version.Split(';');
                                backupList    = new List <Utils.CheckBackup>();
                                for (int i = 0; i < list.Length - 1; i++)
                                {
                                    String[] backup = list[i].Split('%');
                                    backupList.Add(new Utils.CheckBackup(backup[0], backup[1], backup[2]));
                                }

                                box.Dispatcher.Invoke(new Action(() =>
                                {
                                    box.ItemsSource = backupList;
                                }), DispatcherPriority.ContextIdle);
                                cmd = "";
                            }
                            dir.Dispatcher.Invoke(new Action(() =>
                            {
                                dir.Text += "Wait to command\n";
                            }), DispatcherPriority.ContextIdle);
                            if (cmd == null)
                            {
                                _isRunning = false; return;
                            }
                            if (cmd.Equals("+BACKUP"))
                            {
                                dir.Dispatcher.Invoke(new Action(() =>
                                {
                                    dir.Text += "Sono nel backup\n";
                                }), DispatcherPriority.ContextIdle);
                                client.sendFileDimension(xml.Length, ns);
                                //   MessageBox.Show(""+xml.Length);
                                Console.WriteLine("Crash");
                                cmd = client.reciveComand(ns);
                                if (cmd == null)
                                {
                                    _isRunning = false; return;
                                }
                                if (cmd.Equals("+++++OK"))
                                {
                                    Console.WriteLine("Crash dopo OK");
                                    dir.Dispatcher.Invoke(new Action(() =>
                                    {
                                        dir.Text += "Ok \n";
                                    }), DispatcherPriority.ContextIdle);

                                    client.sendData(xml, ns);
                                    Console.WriteLine("Crash Send Data");
                                    dir.Dispatcher.Invoke(new Action(() =>
                                    {
                                        dir.Text += "Send XML \n";
                                    }), DispatcherPriority.ContextIdle);


                                    cmd = client.reciveComand(ns);
                                    if (cmd == null)
                                    {
                                        _isRunning = false; return;
                                    }
                                    Console.WriteLine("Crash Send ok");
                                    if (cmd.Equals("+++++OK"))
                                    {
                                        try
                                        {
                                            cmd = client.reciveComand(ns);
                                        }
                                        catch (IOException ioe)
                                        {
                                            Application.Current.Shutdown();
                                        }
                                        if (cmd == null)
                                        {
                                            _isRunning = false; return;
                                        }
                                        if (cmd.Equals("+UPLOAD"))
                                        {
                                            Console.WriteLine("Crash Upload ok");
                                            try
                                            {
                                                dir.Dispatcher.Invoke(new Action(() =>
                                                {
                                                    dir.Text += "Inizio l'upload\n";
                                                }), DispatcherPriority.ContextIdle);
                                                client.sendData("+++++OK", ns);
                                                int totale = lstFilesFound.Count;
                                                int i      = 0;
                                                foreach (String path in lstFilesFound)
                                                {
                                                    pbStatus.Dispatcher.Invoke(() => { pbStatus.Value = (i * 100) / totale; count.Text = (i * 100) / totale + "%"; }, DispatcherPriority.Background);
                                                    cmd = client.reciveComand(ns);
                                                    if (cmd == null)
                                                    {
                                                        _isRunning = false; return;
                                                    }
                                                    if (cmd.Equals("+++FILE"))
                                                    {
                                                        Console.WriteLine("Crash File");
                                                        dir.Dispatcher.Invoke(new Action(() =>
                                                        {
                                                            dir.Text += "Invio il File " + i + "\n";
                                                        }), DispatcherPriority.ContextIdle);

                                                        client.sendFile(path, ns);
                                                        i++;
                                                        if (client.reciveComand(ns) == "+++++OK")
                                                        {
                                                            client.sendCommand("+++++OK", ns);
                                                        }
                                                    }
                                                }
                                                pbStatus.Dispatcher.Invoke(() => { pbStatus.Value = (totale * 100) / totale; count.Text = (totale * 100) / totale + "%"; }, DispatcherPriority.Background);
                                            }
                                            catch (Exception e)
                                            {
                                                MessageBox.Show("Connesione Persa!");
                                                return;
                                            }
                                        }
                                        clientSend("+++LIST");
                                        cmd = "";
                                    }
                                }
                            }
                            if (cmd == null)
                            {
                                _isRunning = false; return;
                            }
                            if (cmd.Equals("RESTORE"))
                            {
                                dir.Dispatcher.Invoke(new Action(() =>
                                {
                                    dir.Text += "Restoring\n";
                                }), DispatcherPriority.ContextIdle);

                                foreach (Utils.CheckBackup cb in backupList)
                                {
                                    if (cb.CHECK)
                                    {
                                        string toRestore = cb.NAME + @"\" + cb.VERSION;
                                        MessageBox.Show(toRestore);

                                        if (!Directory.Exists(backupFolder + @"\" + cb.NAME + @"\" + cb.VERSION))
                                        {
                                            Directory.CreateDirectory(backupFolder + @"\" + cb.NAME + @"\" + cb.VERSION);
                                        }
                                        client.sendFileDimension(toRestore.Length, client.CLIENT.GetStream());
                                        string mycmd = client.reciveComand(client.CLIENT.GetStream());
                                        Console.WriteLine(mycmd);
                                        if (cmd == null)
                                        {
                                            _isRunning = false; return;
                                        }
                                        if (mycmd.Equals("+++++OK"))
                                        {
                                            client.sendData(toRestore, client.CLIENT.GetStream());
                                            mycmd = client.reciveComand(client.CLIENT.GetStream());
                                            Console.WriteLine(mycmd);
                                            if (cmd == null)
                                            {
                                                _isRunning = false; return;
                                            }
                                            if (mycmd.Equals("+++++OK"))
                                            {
                                                int i        = 0;
                                                Boolean Next = true;
                                                while (Next)
                                                {
                                                    mycmd = client.reciveComand(client.CLIENT.GetStream());
                                                    Console.WriteLine(mycmd);
                                                    if (mycmd == null)
                                                    {
                                                        _isRunning = false; return;
                                                    }
                                                    if (mycmd.Equals("++++END"))
                                                    {
                                                        Console.WriteLine("Finito"); break;
                                                    }
                                                    ;

                                                    if (mycmd.Equals("+++FILE"))
                                                    {
                                                        Console.WriteLine("Numero : " + i);
                                                        i++;
                                                        int dim = client.reciveDimension(client.CLIENT.GetStream());
                                                        client.sendCommand("+++++OK", client.CLIENT.GetStream());
                                                        string relative = client.reciveVersion(dim, client.CLIENT.GetStream());
                                                        Console.WriteLine("path : " + relative);
                                                        client.sendCommand("+++++OK", client.CLIENT.GetStream());
                                                        dim = client.reciveDimension(client.CLIENT.GetStream());
                                                        client.sendCommand("+++++OK", client.CLIENT.GetStream());
                                                        string name = client.reciveVersion(dim, client.CLIENT.GetStream());
                                                        Console.WriteLine("fileName : " + name);
                                                        client.sendCommand("+++++OK", client.CLIENT.GetStream());
                                                        dim = client.reciveDimension(client.CLIENT.GetStream());
                                                        client.sendCommand("+++++OK", client.CLIENT.GetStream());
                                                        client.ReciveFile(Directory.GetCurrentDirectory() + @"\" + backupFolder + @"\" + cb.NAME + @"\" + cb.VERSION + @"\", relative, name, dim, client.CLIENT.GetStream());
                                                        Console.WriteLine("finish\n");

                                                        clientSend("+++++OK");
                                                    }
                                                }
                                                Console.WriteLine("Restore Completato!");
                                                MessageBox.Show("Restore Completato");
                                                Console.WriteLine(cmd);
                                                cmd = "";
                                            }
                                        }
                                    }
                                }
                            }
                        } catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            MessageBox.Show("Connessione persa!");
                            return;
                        }
                    }
                    return;
                }).Start();
                return;
            }
            return;
        }