Esempio n. 1
0
        public ReceivingFileWindow(MainWindow mainWindow, Client client, TcpClient tcpClient)
        {
            this.client      = client;
            this.tcpClient   = tcpClient;
            fileTransferData = client.StartReceiving(tcpClient);

            InitializeComponent();

            if (Properties.Settings.Default.AutoAccept) // if auto-accepting files is enabled
            {
                mainWindow.Ni.BalloonTipTitle = "Transfar";
                mainWindow.Ni.BalloonTipText  = "Receiving file " + fileTransferData.Name + " from " + fileTransferData.HostName;
                mainWindow.Ni.ShowBalloonTip(3000);

                this.Show();
                this.Activate();
                Yes_Button_Click(null, null);
            }
            else
            {
                fileInfo.Text = "Do you want to receive file " + fileTransferData.Name + " from " + fileTransferData.HostName + '?';
                this.Show();
                this.Activate();
            }
        }
Esempio n. 2
0
        private async void StartSending()
        {
            cts = new CancellationTokenSource();
            var progressIndicator = new Progress <double>(ReportProgress);

            try
            {
                fileTransferData = server.StartSending(filePath, selectedClient);
            }
            catch (SocketException)
            {
                MessageBox.Show("The selected host is unavailable.", "Transfar", MessageBoxButton.OK,
                                MessageBoxImage.Stop, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
                this.Close();
                return;
            }
            originalLength = fileTransferData.Length;

            // If there are no issues with the transfer I can show the window
            fileInfo.Text = "Sending file " + fileTransferData.Name + " to " + fileTransferData.HostName + "...";
            this.Show();
            this.Activate();

            try
            {
                await SendFileAsync(progressIndicator, cts.Token);
            }
            catch (Exception)
            {
                Console.WriteLine("Cancellation requested!");
            }

            this.Close();
        }
Esempio n. 3
0
        /*
         * Sends a chunk of data (to be in a while loop)
         */
        public void Send(FileTransferData fileTransferData)
        {
            var buffer = new byte[256 * 1024];
            int bytesRead;

            if (((bytesRead = fileTransferData.FileStream.Read(buffer, 0, buffer.Length)) > 0) && (fileTransferData.Length > 0))
            {
                fileTransferData.NetworkStream.Write(buffer, 0, bytesRead);
                fileTransferData.Length -= bytesRead;
            }
        }
Esempio n. 4
0
        public FileTransferData StartSending(string filePath, NamedIPEndPoint selectedClient)
        {
            FileTransferData fileTransferData = new FileTransferData();

            fileTransferData.HostName = selectedClient.Name;

            TcpClient tcpClient = new TcpClient();

            tcpClient.Connect(selectedClient.EndPoint); // Connecting to the client specified (throws an exception if not available)

            if (Directory.Exists(filePath))
            {
                string tempPath = Path.GetTempPath() + new DirectoryInfo(filePath).Name + ".zip";
                if (File.Exists(tempPath)) // Check if the file already exists so that ZipFile doesn't throw an exception
                {
                    File.Delete(tempPath);
                }
                ZipFile.CreateFromDirectory(filePath, tempPath);
                filePath = tempPath;
            }
            FileInfo fi = new FileInfo(filePath); // Obtaining the infos of the specified file

            fileTransferData.Name   = fi.Name;
            fileTransferData.Length = fi.Length;
            Debug.WriteLine("[SERVER] File length of the sent file: " + fileTransferData.Length);
            Debug.WriteLine("[SERVER] File name of the sent file: " + fileTransferData.Name);

            fileTransferData.NetworkStream = tcpClient.GetStream();
            fileTransferData.NetworkStream.WriteTimeout = 20000;

            byte[] hostNameLengthBuffer = BitConverter.GetBytes(Encoding.Unicode.GetByteCount(Environment.UserName));
            fileTransferData.NetworkStream.Write(hostNameLengthBuffer, 0, hostNameLengthBuffer.Length);

            byte[] hostNameBuffer = Encoding.Unicode.GetBytes(Environment.UserName);
            fileTransferData.NetworkStream.Write(hostNameBuffer, 0, hostNameBuffer.Length);


            byte[] fileNameLengthBuffer = BitConverter.GetBytes(Encoding.Unicode.GetByteCount(fileTransferData.Name));
            fileTransferData.NetworkStream.Write(fileNameLengthBuffer, 0, fileNameLengthBuffer.Length);

            byte[] fileNameBuffer = Encoding.Unicode.GetBytes(fileTransferData.Name);
            fileTransferData.NetworkStream.Write(fileNameBuffer, 0, fileNameBuffer.Length);


            byte[] fileLengthBuffer = BitConverter.GetBytes(fileTransferData.Length);
            fileTransferData.NetworkStream.Write(fileLengthBuffer, 0, fileLengthBuffer.Length);
            fileTransferData.FileStream = File.OpenRead(filePath);

            Debug.WriteLine("[SERVER] Initial file data sent successfully");

            return(fileTransferData);
        }
Esempio n. 5
0
        /*
         * Starts receiving the first metadata of the file (host name, file name and file size).
         */
        public FileTransferData StartReceiving(TcpClient client)
        {
            FileTransferData fileTransferData = new FileTransferData();
            NetworkStream    netStream        = client.GetStream();

            //netStream.ReadTimeout = 5000; // TODO: Not working, find another way to realize that the sender has stopped

            Console.WriteLine("[CLIENT] Receiving new file...");

            byte[] hostNameLengthBuffer = new byte[sizeof(int)];
            netStream.Read(hostNameLengthBuffer, 0, hostNameLengthBuffer.Length);
            int hostNameLength = BitConverter.ToInt32(hostNameLengthBuffer, 0);

            byte[] hostNameBuffer = new byte[hostNameLength];
            netStream.Read(hostNameBuffer, 0, hostNameBuffer.Length);


            byte[] fileNameLengthBuffer = new byte[sizeof(int)];
            netStream.Read(fileNameLengthBuffer, 0, fileNameLengthBuffer.Length);
            int fileNameLength = BitConverter.ToInt32(fileNameLengthBuffer, 0);

            byte[] fileNameBuffer = new byte[fileNameLength];
            netStream.Read(fileNameBuffer, 0, fileNameBuffer.Length);


            byte[] fileLengthBuffer = new byte[sizeof(long)];
            netStream.Read(fileLengthBuffer, 0, fileLengthBuffer.Length);


            string hostName   = Encoding.Unicode.GetString(hostNameBuffer);
            string fileName   = Encoding.Unicode.GetString(fileNameBuffer);
            long   fileLength = BitConverter.ToInt64(fileLengthBuffer, 0);

            Console.WriteLine("[CLIENT] Received host name: " + hostName);
            Console.WriteLine("[CLIENT] Received file name: " + fileName);
            Console.WriteLine("[CLIENT] Received length: " + fileLength);

            DirectoryInfo di = Directory.CreateDirectory(Path);

            fileTransferData.HostName      = hostName;
            fileTransferData.Name          = fileName;
            fileTransferData.Path          = Path + "//" + fileName;
            fileTransferData.Length        = fileLength;
            fileTransferData.NetworkStream = netStream;
            fileTransferData.FileStream    = null; //  File.Create(fileTransferData.Path); that's because the management is done on the GUI
            return(fileTransferData);
        }
Esempio n. 6
0
        /*
         * Receive a chunk of data (to be in a while loop)
         */
        public void Receive(FileTransferData fileTransferData)
        {
            //fileTransferData.NetworkStream.ReadTimeout = 5000; // HACK: not working

            var buffer = new byte[256 * 1024];
            int bytesRead;

            if (((bytesRead = fileTransferData.NetworkStream.Read(buffer, 0, buffer.Length)) > 0) && (fileTransferData.Length > 0))
            {
                fileTransferData.FileStream.Write(buffer, 0, bytesRead);
                fileTransferData.Length -= bytesRead;
            }
            else
            {
                throw new SocketException(1);
            }
        }
Esempio n. 7
0
 /*
  * Stops the file reception and deletes the file stored.
  */
 public void CancelReceiving(FileTransferData fileTransferData)
 {
     fileTransferData.NetworkStream.Dispose();
     fileTransferData.FileStream?.Dispose();
     File.Delete(fileTransferData.Path);
 }
Esempio n. 8
0
 /*
  * Ends gracefully the file reception.
  */
 public void EndReceiving(FileTransferData fileTransferData)
 {
     fileTransferData.FileStream.Flush();
     fileTransferData.NetworkStream.Dispose();
     fileTransferData.FileStream.Dispose();
 }
Esempio n. 9
0
 /*
  * Stops the file transfer.
  */
 public void CancelSending(FileTransferData fileTransferData)
 {
     fileTransferData.NetworkStream.Dispose();
     fileTransferData.FileStream.Dispose();
 }