void receiveLoop() { while (connected) { byte[] dataByte = null; int pos = 0; int read = 1; try { byte[] lenByte = new byte[4]; while (pos < lenByte.Length) { read = socket.Receive(lenByte, pos, lenByte.Length - pos); pos += read; App.globalDownCounter.addBytes((ulong)read); } pos = 0; App.globalDownCounter.addBytes(4); dataByte = new byte[BitConverter.ToInt32(lenByte, 0)]; if (dataByte.Length > 0) { while (pos < dataByte.Length) { read = socket.Receive(dataByte, pos, dataByte.Length - pos); pos += read; App.globalDownCounter.addBytes((ulong)read); } } } catch { continue; } Commands.Command c = App.serializer.deserialize(dataByte); if (c is Commands.DataCommand) { pos = 0; read = 1; App.speedLimiter.limitDownload((ulong)((Commands.DataCommand)c).data.Length); byte[] chunk = new byte[((Commands.DataCommand)c).dataLength]; while (pos < chunk.Length) { read = socket.Receive(chunk, pos, (int)App.speedLimiter.limitDownload((ulong)(chunk.Length - pos), rateLimiterDisabled)); pos += read; App.globalDownCounter.addBytes((ulong)read); } ((Commands.DataCommand)c).data = chunk; } if (c is Commands.HelloCommand) { hello = (Commands.HelloCommand)c; } while (commandReceived == null && connected) { System.Threading.Thread.Sleep(10); } commandReceived?.Invoke(c, this); } }
public void ListenOnce() { byte[] readBuffer = new byte[1]; // Udt connections are already established because we need the udt ping to keep the tunnel alive, therefore we need a new "listen" Listening = true; int read = UdtConnection.Receive(readBuffer, 0, 1); if (read == 1 && readBuffer[0] == PACKET_CONNECTION_RQ_TYPE) { Logger.Debug("[" + Thread.CurrentThread.ManagedThreadId + "] Received a connection from " + UdtConnection.RemoteEndPoint.Address); } else { throw new Exception("Failed to get a connection"); } Connected = true; Listening = false; }
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 Send_receive() { ManualResetEvent serverDoneEvent = new ManualResetEvent(false); ManualResetEvent clientDoneEvent = new ManualResetEvent(false); int port = _portNum++; 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()) { accept.Send(new byte[] { 1, 2, 3 }); byte[] buffer = new byte[1024]; Assert.AreEqual(3, accept.Receive(buffer)); serverDoneEvent.Set(); Assert.IsTrue(clientDoneEvent.WaitOne(1000)); } } }); using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { client.Connect(IPAddress.Loopback, port); byte[] buffer = new byte[1024]; Assert.AreEqual(3, client.Receive(buffer)); CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, buffer.Take(3)); client.Send(new byte[] { 1, 2, 3 }); clientDoneEvent.Set(); Assert.IsTrue(serverDoneEvent.WaitOne(1000)); } serverTask.Wait(); }
public void SendFile_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"); 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)) { byte[] buffer = new byte[1024]; client.Connect(IPAddress.Loopback, port); Assert.AreEqual(44, client.Receive(buffer)); CollectionAssert.AreEqual(File.ReadAllBytes(path), buffer.Take(44)); clientDoneEvent.Set(); Assert.IsTrue(serverDoneEvent.WaitOne(1000)); } serverTask.Wait(); }
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); } }
void receiveLoop() { while (connected) { byte[] dataByte = null; int pos = 0; int read = 1; try { byte[] lenByte = new byte[4]; while (pos < lenByte.Length) { read = socket.Receive(lenByte, pos, lenByte.Length - pos); pos += read; App.globalDownCounter.addBytes((ulong)read); } pos = 0; App.globalDownCounter.addBytes((ulong)lenByte.Length); dataByte = new byte[BitConverter.ToInt32(lenByte, 0)]; if (dataByte.Length > 0) { while (pos < dataByte.Length) { read = socket.Receive(dataByte, pos, dataByte.Length - pos); pos += read; App.globalDownCounter.addBytes((ulong)read); } } } catch { continue; } Commands.Command c = App.serializer.deserialize(dataByte); if (c is Commands.DataCommand) { pos = 0; read = 1; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); byte[] chunk = new byte[((Commands.DataCommand)c).dataLength]; while (pos < chunk.Length) { read = socket.Receive(chunk, pos, (int)App.speedLimiter.limitDownload((ulong)(chunk.Length - pos), rateLimiterDisabled)); pos += read; App.globalDownCounter.addBytes((ulong)read); } ((Commands.DataCommand)c).data = chunk; sw.Stop(); if (sw.ElapsedTicks > 0) { rate = (ulong)((chunk.Length) / (sw.ElapsedTicks / (double)System.Diagnostics.Stopwatch.Frequency)); } } while (commandReceived == null && connected) { System.Threading.Thread.Sleep(10); } commandReceived?.Invoke(c); } }