Ejemplo n.º 1
0
        private void ReceiveFile(NetworkManager manager, string path, string[] fileData)
        {
            NetworkStream stream = manager.GetStream();
            int length = Convert.ToInt32(fileData[0]);
            byte[] buffer = new byte[length];
            int received = 0;
            int read = 0;
            int size = 1024;
            int remaining = 0;

            while (received < length)
            {
                remaining = length - received;
                if (remaining < size)
                {
                    size = remaining;
                }
                read = stream.Read(buffer, received, size);
                received += read;
                SetProgress(received);
            }
            string writepath = Path.Combine(path, fileData[1]);
            using (FileStream fStream = new FileStream(writepath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, buffer.Length))
            {
                fStream.Write(buffer, 0, buffer.Length);
            }
            string[] temp = ReadConfig(Selector1.SelectedItem.ToString());
            var a = ProgramLaunchCheckBox.Checked ? ProgramName.Text : "null";
            CreateOrChangeConfig(Selector1.SelectedItem.ToString(), AdressBox.Text, temp[2], fileData[1], a);
            if (fileData[1].EndsWith(".zip"))
            {
                try
                {
                    ZipFile.ExtractToDirectory(writepath, path);
                }
                catch (Exception)
                {
                }
                File.Delete(writepath);
            }
            VersionLabel.Text = fileData[1];
        }
Ejemplo n.º 2
0
 private NetworkManager GetManager()
 {
     IPAddress address = null;
     NetworkManager manager = new NetworkManager();
     try
     {
         address = Dns.GetHostAddresses(AdressBox.Text)[0];
     }
     catch (Exception)
     {
         try
         {
             address = IPAddress.Parse(AdressBox.Text);
         }
         catch (Exception)
         {
             address = IPAddress.Parse("127.0.0.1");
         }
     }
     finally
     {
         try
         {
             var client = new TcpClient(address.ToString(), 56931);
             NetworkStream stream = client.GetStream();
             manager = new NetworkManager(stream);
         }
         catch (Exception)
         {
             MessageBox.Show("The server is not online!");
             Environment.Exit(0);
         }
     }
     return manager;
 }