Exemplo n.º 1
0
        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)";
        }
Exemplo n.º 2
0
 private void SendFileButton_Click(object sender, System.Windows.RoutedEventArgs e)
 {
     try
     {
         var             fileDialog = new OpenFileDialog();
         Nullable <bool> result     = fileDialog.ShowDialog(this);
         if (result == true)
         {
             DisableClientServerButtons();
             string            filename = fileDialog.FileName;
             Udt.StdFileStream fs       = new Udt.StdFileStream(filename, FileMode.Open);
             byte[]            buffer   = new byte[16834];
             _socket.Connect();
             Status.Text = "Sending size " + fs.Length;
             _socket.Send(BitConverter.GetBytes(fs.Length), sizeof(long));
             int read = fs.Read(buffer, 0, 16834);
             Status.Text = "Sending filename " + filename;
             while (read > 0)
             {
                 _socket.Send(buffer, read);
                 read = fs.Read(buffer, 0, 16834);
             }
             fs.Close();
             _socket.Close();
             Status.Text = "Sent " + filename;
             SendFileButton.IsEnabled = false;
         }
     }
     catch (Exception ex)
     {
         Dispatcher.Invoke(new Action(() => ErrorBlock.Text = ex.Message + "\n" + ex.StackTrace));
     }
 }
Exemplo n.º 3
0
        public void ReceiveFile_stream()
        {
            ManualResetEvent serverDoneEvent = new ManualResetEvent(false);
            ManualResetEvent clientDoneEvent = new ManualResetEvent(false);
            int    port        = _portNum++;
            string path        = GetFile("The quick brown fox jumped over the lazy dog");
            string receivePath = GetFile();

            var serverTask = Task.Factory.StartNew(() =>
            {
                using (Udt.Socket server = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream))
                {
                    server.Bind(IPAddress.Loopback, port);
                    server.Listen(1);

                    using (Udt.Socket accept = server.Accept())
                        using (Udt.StdFileStream file = new Udt.StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            accept.SendFile(file);

                            serverDoneEvent.Set();
                            Assert.IsTrue(clientDoneEvent.WaitOne(1000));
                        }
                }
            });

            using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream))
                using (Udt.StdFileStream file = new Udt.StdFileStream(receivePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    client.Connect(IPAddress.Loopback, port);
                    Assert.AreEqual(44, client.ReceiveFile(file, 44));

                    file.Close();

                    CollectionAssert.AreEqual(File.ReadAllBytes(path), File.ReadAllBytes(receivePath));

                    clientDoneEvent.Set();
                    Assert.IsTrue(serverDoneEvent.WaitOne(1000));
                }

            serverTask.Wait();
        }
Exemplo n.º 4
0
 private void SendFileButton_Click(object sender, System.Windows.RoutedEventArgs e)
 {
     try
     {
         var fileDialog = new OpenFileDialog();
         Nullable<bool> result = fileDialog.ShowDialog(this);
         if (result == true)
         {
             DisableClientServerButtons();
             string filename = fileDialog.FileName;
             Udt.StdFileStream fs = new Udt.StdFileStream(filename, FileMode.Open);
             byte[] buffer = new byte[16834];
             _socket.Connect();
             Status.Text = "Sending size " + fs.Length;
             _socket.Send(BitConverter.GetBytes(fs.Length), sizeof(long));
             int read = fs.Read(buffer, 0, 16834);
             Status.Text = "Sending filename " + filename;
             while (read > 0)
             {
                 _socket.Send(buffer, read);
                 read = fs.Read(buffer, 0, 16834);
             }
             fs.Close();
             _socket.Close();
             Status.Text = "Sent " + filename;
             SendFileButton.IsEnabled = false;
         }
     }
     catch (Exception ex)
     {
         Dispatcher.Invoke(new Action(() => ErrorBlock.Text = ex.Message + "\n" + ex.StackTrace));
     }
 }
Exemplo n.º 5
0
 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)";
 }
Exemplo n.º 6
0
        public void ReceiveFile_stream()
        {
            ManualResetEvent serverDoneEvent = new ManualResetEvent(false);
            ManualResetEvent clientDoneEvent = new ManualResetEvent(false);
            int port = _portNum++;
            string path = GetFile("The quick brown fox jumped over the lazy dog");
            string receivePath = GetFile();

            var serverTask = Task.Factory.StartNew(() =>
            {
                using (Udt.Socket server = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream))
                {
                    server.Bind(IPAddress.Loopback, port);
                    server.Listen(1);

                    using (Udt.Socket accept = server.Accept())
                    using (Udt.StdFileStream file = new Udt.StdFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        accept.SendFile(file);

                        serverDoneEvent.Set();
                        Assert.IsTrue(clientDoneEvent.WaitOne(1000));
                    }
                }
            });

            using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream))
            using (Udt.StdFileStream file = new Udt.StdFileStream(receivePath, FileMode.Open, FileAccess.ReadWrite))
            {
                client.Connect(IPAddress.Loopback, port);
                Assert.AreEqual(44, client.ReceiveFile(file, 44));

                file.Close();

                CollectionAssert.AreEqual(File.ReadAllBytes(path), File.ReadAllBytes(receivePath));

                clientDoneEvent.Set();
                Assert.IsTrue(serverDoneEvent.WaitOne(1000));
            }

            serverTask.Wait();
        }