Exemplo n.º 1
0
        private void скачатиФайлToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileWindow.FileName = this.GetFileName(fileForDownload);

            DialogResult result = saveFileWindow.ShowDialog();

            if (result == DialogResult.Cancel)
            {
                return;
            }

            InfoFileNew infoFile = new InfoFileNew();

            infoFile._path = fileForDownload;
            //else get and save file from server
            new Task(() =>
            {
                Task setTextStatus = new Task(() =>
                {
                    for (; ;)
                    {
                        statusLable.Text = "Отримання файлу";
                        for (int i = 0; i < 3; i++)
                        {
                            statusLable.Text += ".";
                            Thread.Sleep(500);
                        }
                    }
                });
                setTextStatus.Start();
                byte[] file = GetInfoAboutFileFromServer(infoFile, 2004);
                File.WriteAllBytes(saveFileWindow.FileName, file);
                setTextStatus.Dispose();
            }).Start();
        }
Exemplo n.º 2
0
        //Method Send Files to Server
        static byte[] GetInfoAboutFileFromServer(InfoFileNew path, int port)
        {
            // Соединяемся с удаленным устройством

            // Устанавливаем удаленную точку для сокета
            IPHostEntry ipHost     = Dns.GetHostEntry("localhost");
            IPAddress   ipAddr     = ipHost.AddressList[0];
            IPEndPoint  ipEndPoint = new IPEndPoint(ipAddr, port);

            ////UdpClient sender = new UdpClient();
            //// Поля, связанные с UdpClient
            //IPAddress remoteIPAddress = IPAddress.Parse("127.0.0.1");
            //UdpClient sender = new UdpClient();
            //IPEndPoint ipEndPoint = new IPEndPoint(remoteIPAddress, port);

            Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // Соединяем сокет с удаленной точкой
            try
            {
                sender.Connect(ipEndPoint);

                sender.Send(path.PackToXml());
                byte[] receiveBytes = new byte[5000];

                int bytesRec = sender.Receive(receiveBytes);

                InfoFileNew infoFile = receiveBytes.UnpackFromXml <InfoFileNew>();


                byte[] receiveBytes1 = new byte[infoFile._sizeFile];

                int bytesRec1 = sender.Receive(receiveBytes1);

                // Освобождаем сокет
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

                return(receiveBytes1);
            }
            catch (SocketException ex)
            {
                MessageBox.Show($"Не має підключення до сервера!\n{ex.Message}", "Попередження!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(null);
            }
        }
Exemplo n.º 3
0
        private async void завантажитиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream      myStream = null;
            InfoFileNew infoFile = new InfoFileNew();

            byte[] result;
            if (this.openFileWindow.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileWindow.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            result = new byte[myStream.Length];
                            await myStream.ReadAsync(result, 0, (int)myStream.Length);

                            infoFile._fileName = Path.GetFileName(this.openFileWindow.FileName);
                            infoFile._path     = currentDirectory;
                            infoFile._sizeFile = myStream.Length;
                            if (myStream != null)
                            {
                                myStream.Close();
                            }

                            //MessageBox.Show(sendData.ToList().Count.ToString());
                            SendInfoAboutFileToServer(infoFile, result, 2003);
                            //string tmpPath = Encoding.UTF8.GetString(itemsSelected[i].Tag as byte[]);
                            Thread.Sleep(40);

                            GetListItemsFromServer(currentDirectory);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. \nOriginal error: " + ex.Message);
                    if (myStream != null)
                    {
                        myStream.Close();
                    }
                }
            }
        }
Exemplo n.º 4
0
        //Method Send Files to Server
        static void SendInfoAboutFileToServer(InfoFileNew infoAboutFile, byte[] result, int port = 2003)
        {
            // Соединяемся с удаленным устройством

            // Устанавливаем удаленную точку для сокета
            IPHostEntry ipHost     = Dns.GetHostEntry("localhost");
            IPAddress   ipAddr     = ipHost.AddressList[0];
            IPEndPoint  ipEndPoint = new IPEndPoint(ipAddr, port);

            ////UdpClient sender = new UdpClient();
            //// Поля, связанные с UdpClient
            //IPAddress remoteIPAddress = IPAddress.Parse("127.0.0.1");
            //UdpClient sender = new UdpClient();
            //IPEndPoint ipEndPoint = new IPEndPoint(remoteIPAddress, port);

            Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // Соединяем сокет с удаленной точкой
            try
            {
                sender.Connect(ipEndPoint);

                Byte[] bytes = infoAboutFile.PackToXml();

                //Console.WriteLine("Отправка деталей файла...");

                // Отправляем информацию о файле
                sender.Send(bytes);

                Thread.Sleep(2000);

                sender.Send(result);
                // Освобождаем сокет
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            }
            catch (SocketException ex)
            {
                MessageBox.Show($"Не має підключення до сервера!\n{ex.Message}", "Попередження!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }