示例#1
0
        public void TransferRequest_Test()
        {
            var fn = "Boo";

            using (var ms = new MemoryStream())
            {
                SendReceiveHelper.SendTransferRequest(ms, new TransferFileJobRequest
                {
                    FileName = fn,
                    Size     = 33,
                });

                ms.Position = 0;

                var req = SendReceiveHelper.GetTransferRequest(ms);

                Assert.AreEqual(req.Size, 33UL);
                Assert.AreEqual(req.FileName, fn);
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                Console.CancelKeyPress += cancelHandler;

                listener = new TcpListener(IPAddress.Parse("0.0.0.0"), PORT);
                listener.Start();

                Console.WriteLine($"Waiting for connection at :{PORT}");
                Console.Write("Press any key, or 'C' to quit");
                while (true)
                {
                    using (var client = listener.AcceptTcpClient())
                        using (var stream = client.GetStream())
                        {
                            Console.WriteLine("connected");
                            var request = SendReceiveHelper.GetTransferRequest(stream);

                            Console.WriteLine("Got file name {0} for {1}", request.FileName, request.DownloadFromServer?"download":"upload");
                            var isReceive = !request.DownloadFromServer;
                            if (isReceive)
                            {
                                SendReceiveHelper.SendTransferFileJobResponse(stream, new TransferFileJobResponse()
                                {
                                    Size   = request.Size,
                                    Status = 0,
                                });

                                using (new AutoStopwatch((long)request.Size))
                                {
                                    ReceiveFile(stream, request.FileName, (long)request.Size);
                                }
                            }
                            else
                            {
                                var fi = new FileInfo(request.FileName);
                                if (fi.Exists)
                                {
                                    SendReceiveHelper.SendTransferFileJobResponse(stream, new TransferFileJobResponse()
                                    {
                                        Size   = (ulong)fi.Length,
                                        Status = 0,
                                    });

                                    using (new AutoStopwatch(fi.Length))
                                    {
                                        SendFile(stream, fi);
                                    }
                                }
                                else
                                {
                                    SendReceiveHelper.SendTransferFileJobResponse(stream, new TransferFileJobResponse()
                                    {
                                        Size   = 0,
                                        Status = 1,
                                    });
                                }
                            }

                            Console.WriteLine("Done {0}", request.IsDone ? "stopping server." : "");
                            if (request.IsDone)
                            {
                                break;
                            }
                        }
                }
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Interrupted {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught {0}", ex.Message);
            }
            finally
            {
                listener?.Stop();
            }
        }