static int Main(string[] args) { if ((args.Length != 4) || (0 == int.Parse(args[1]))) { Console.WriteLine("Usage: ReceiveFile server_ip server_port remote_filename local_filename"); return(1); } try { using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { client.Connect(IPAddress.Parse(args[0]), int.Parse(args[1])); // Send name information of the requested file string name = args[2]; byte[] nameBytes = Encoding.UTF8.GetBytes(name); client.Send(BitConverter.GetBytes(nameBytes.Length), 0, sizeof(int)); client.Send(nameBytes); // Get size information long size; byte[] file = new byte[1024]; client.Receive(file, 0, sizeof(long)); size = BitConverter.ToInt64(file, 0); // Receive the file string localName = args[3]; client.ReceiveFile(localName, size); client.Close(); } Console.ReadKey(true); return(0); } catch (Exception ex) { Console.Error.WriteLine("Error receiving file: {0}", ex.Message); Console.ReadKey(true); return(2); } }
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(); }
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(); }