Beispiel #1
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();
        }
Beispiel #2
0
        internal static void SendFiles(NetIncomingMessage msg)
        {
            Messages message = Messages.FromByteArray(msg.Data);
            Guid id = new Guid((string)message.Data);

            //Messages outGoingMessage = new Messages();
            //outGoingMessage.MessageType = Message.RecieveFile;
            //NetOutgoingMessage outgoingMessage2 = server.CreateMessage();
            //outgoingMessage2.Write(outGoingMessage.ToByteArray());
            //server.SendUnconnectedMessage(outgoingMessage2, msg.SenderEndPoint);

            Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
            Udt.StdFileStream fs = new Udt.StdFileStream(userFileList.GetFilePathFromGuid(id), FileMode.Open);
            {
                socket.BlockingSend = true;
                socket.Connect(msg.SenderEndPoint.Address, 10000);
                // Send the file length, in bytes
                socket.Send(BitConverter.GetBytes(fs.Length), 0, sizeof(long));
                // Send the file contents
                socket.SendFile(fs);

            }
        }
Beispiel #3
0
        static int Main(string[] args)
        {
            if ((1 < args.Length) || ((1 == args.Length) && (0 == int.Parse(args[0]))))
            {
                Console.WriteLine("Usage: SendFile [ServerPort]");
                return(1);
            }

            try
            {
                using (Udt.Socket server = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream))
                {
                    int port = 9000;
                    if (1 == args.Length)
                    {
                        port = int.Parse(args[0]);
                    }

                    server.Bind(IPAddress.Any, port);
                    Console.WriteLine("Server is ready at port: {0}", port);
                    server.Listen(1);

                    using (Udt.Socket client = server.Accept())
                    {
                        server.Close();

                        // Receive file name from client
                        byte[] file = new byte[1024];
                        int    length;
                        string name;

                        client.Receive(file, 0, sizeof(int));
                        length = BitConverter.ToInt32(file, 0);

                        client.Receive(file, 0, length);
                        name = Encoding.UTF8.GetString(file, 0, length);

                        // Send file size information
                        client.Send(BitConverter.GetBytes(new FileInfo(name).Length), 0, sizeof(long));

                        Udt.TraceInfo trace = client.GetPerformanceInfo();

                        // Send the file
                        client.SendFile(name);

                        trace = client.GetPerformanceInfo();

                        PrintProps("Total", trace.Total);
                        PrintProps("Local", trace.Local);
                        PrintProps("Probe", trace.Probe);

                        client.Close();
                    }
                }

                Console.ReadKey(true);
                return(0);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error sending file: {0}", ex.Message);
                Console.ReadKey(true);
                return(2);
            }
        }