Пример #1
0
        private async Task SendFile()
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter          = "压缩文件|*.zip;*.jar;*.rar";//文件扩展名
            dialog.CheckFileExists = true;
            dialog.ShowDialog();
            if (!string.IsNullOrEmpty(dialog.FileName))//可以上传压缩包.zip 或者jar包
            {
                try
                {
                    byte[] buffer = UdpHelper.FileToBytes(dialog.FileName);//文件转成byte二进制数组

                    var packages = new UdpHelper(65000).PackageSlice(buffer, 1, UdpFileModel.File);
                    var c        = 0;

                    foreach (var package in packages)
                    {
                        await _client.SendAsync(package, package.Length);

                        LbSendFileProcess.Content = $"{++c}/{packages.Count}";
                        await Task.Delay(1);
                    }
                    UdpHelper.ClearPackages(1);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Пример #2
0
        private async Task SendImage()
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Title            = "选择文件";
            openFileDialog.Filter           = "jpg|*.jpg|jpeg|*.jpeg|*.txt";
            openFileDialog.FileName         = string.Empty;
            openFileDialog.FilterIndex      = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.DefaultExt       = "jpg";
            bool?result = openFileDialog.ShowDialog();

            if (result != true)
            {
                return;
            }
            string fileName = openFileDialog.FileName;

            var buffer = UdpHelper.ImageToBytes(fileName);

            ImageSend.Source = UdpHelper.BytesToBitmapImage(buffer);

            var packages = new UdpHelper(65000).PackageSlice(buffer, 1, UdpFileModel.Image);
            var c        = 0;

            foreach (var package in packages)
            {
                await _client.SendAsync(package, package.Length);

                LbSendImageProcess.Content = $"{++c}/{packages.Count}";
                await Task.Delay(1);
            }
        }
Пример #3
0
        private void ReceiveFile()
        {
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);

            //var udpClient = new System.Net.Sockets.UdpClient();
            while (true)
            {
                Byte[] receiveBytes = receivingUdpClient.Receive(ref remoteIpEndPoint);
                //var name = Thread.CurrentThread.ManagedThreadId.ToString();
                try
                {
                    //Dispatcher.Invoke(() =>
                    //{
                    //    TbReceiveContext.Text += name + Environment.NewLine;
                    //    TbReceiveContext.ScrollToEnd();
                    //});
                    //Byte[] receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
                    var op = _udpHelper.PackageConcat(receiveBytes);
                    if (op == 0)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            //LbReceive.Content = $"{++count + 1}";
                            LbReceiveFileProcess.Content = $"{++count}/{UdpHelper.Packages[0].PackageCount}";
                        });
                    }
                    if (op == 1)
                    {
                        var buffer = _udpHelper.PackageMerge(UdpHelper.Packages, 1);
                        Dispatcher.Invoke(() =>
                        {
                            //LbReceiveFileProcess.Content = $"{++count}/{UdpHelper.Packages[0].PackageCount}";
                            UdpHelper.BytesToFile(buffer, $"E://1.zip");
                            UdpHelper.ClearPackages(1);
                            MessageBox.Show("接收成功!");
                            count = 0;
                        });
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
Пример #4
0
        private void ReceiveImage()
        {
            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            while (true)
            {
                lock (locker)
                {
                    try
                    {
                        Byte[] receiveBytes = receivingUdpClient.Receive(ref remoteIpEndPoint);
                        var    op           = _udpHelper.PackageConcat(receiveBytes);
                        if (op == 0)
                        {
                            Dispatcher.Invoke(() =>
                            {
                                //LbReceive.Content = $"{++count + 1}";
                                LbReceiveImageProcess.Content = $"{++count}/{UdpHelper.Packages.Count}";
                            });
                        }
                        if (op == 1)
                        {
                            var img = _udpHelper.PackageMerge(UdpHelper.Packages, 1);
                            Dispatcher.Invoke(() =>
                            {
                                ImageReceive.Source = UdpHelper.BytesToBitmapImage(img);
                                count = 0;
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }
        }