BlitsMe.UDTTester.MainWindow.ReceiveFileButton_Click C# (CSharp) Method

ReceiveFileButton_Click() private method

private ReceiveFileButton_Click ( object sender, System e ) : void
sender object
e System
return void
        private void ReceiveFileButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            DisableClientServerButtons();
            byte[] buffer = new byte[sizeof(long)];
            _socket.ListenOnce();
            _socket.Read(buffer, sizeof(long));
            long length = BitConverter.ToInt64(buffer, 0);
            Status.Text = Status.Text + " (" + length + " bytes)";
            // Receive the file contents (path is where to store the file)
            var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "FileSend.bin");
            Udt.StdFileStream fs = new Udt.StdFileStream(path, FileMode.Create);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var fsBuffer = new byte[16834];
            int totalRead = 0;
            int read = totalRead = _socket.Read(fsBuffer, 16834);
            while (totalRead < length)
            {
                fs.Write(fsBuffer, 0, read);
                totalRead += read = _socket.Read(fsBuffer, (int)((length - totalRead > 16834) ? 16834 : length - totalRead));
            }
            fs.Close();
            _socket.Close();
            sw.Stop();
            Status.Text = "File Received, " + length + " bytes in " + sw.Elapsed.TotalSeconds + " seconds (" + (sw.Elapsed.TotalSeconds == 0 ? "?" : (length / 1024 / sw.Elapsed.TotalSeconds).ToString()) + "kBps)";
        }