protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { if (client != null) { client.sendCommand("++CLOSE", client.CLIENT.GetStream()); client.close(); } base.OnClosing(e); }
private string ConnectServer() { if (this.client == null) { try { TcpClient tmp = 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 str = login(); client.close(); client = null; return(str); //t = new Thread(new ParameterizedThreadStart(threadProc)); // //t.Start(client); //command = "++LOGIN"; } catch (SocketException e1) { MessageBox.Show("Server non ragiungibile!"); this.client = null; return(null); } catch (FormatException e2) { MessageBox.Show("Porta errata!"); this.client = null; return(null); } catch (InvalidOperationException e3) { MessageBox.Show("Indirizzo IP errato!"); this.client = null; return(null); } } else { return(null); } }
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; }