public override void send(Commands.Command c) { if (c is Commands.DataCommand) { ((Commands.DataCommand)c).dataLength = ((Commands.DataCommand)c).data.Length; } byte[] b = App.serializer.serialize(c); int len = b.Length; lock (sendLock) { try { int pos = 0; int read = 1; while (pos < 4) { read = socket.Send(BitConverter.GetBytes(len), pos, 4 - pos); App.globalUpCounter.addBytes((ulong)read); pos += read; } pos = 0; App.globalUpCounter.addBytes(4); while (pos < b.Length) { read = socket.Send(b, pos, b.Length - pos); App.globalUpCounter.addBytes((ulong)read); pos += read; } if (c is Commands.DataCommand) { b = ((Commands.DataCommand)c).data; pos = 0; read = 1; while (pos < b.Length) { int amt = (int)App.speedLimiter.limitUpload((ulong)(b.Length - pos), rateLimiterDisabled); read = socket.Send(b, pos, amt); App.globalUpCounter.addBytes((ulong)read); pos += read; } } } catch { return; } } }
static internal void Run(Udt.Socket conn, string file, bool bVerbose) { int ini = Environment.TickCount; using (Udt.NetworkStream netStream = new Udt.NetworkStream(conn)) using (BinaryWriter writer = new BinaryWriter(netStream)) using (BinaryReader reader = new BinaryReader(netStream)) using (FileStream fileReader = new FileStream(file, FileMode.Open, FileAccess.Read)) { long fileSize = new FileInfo(file).Length; writer.Write(Path.GetFileName(file)); writer.Write(fileSize); byte[] buffer = new byte[512 * 1024]; long pos = 0; int i = 0; ConsoleProgress.Draw(i++, pos, fileSize, ini, Console.WindowWidth / 3); while (pos < fileSize) { int toSend = buffer.Length < (fileSize - pos) ? buffer.Length : (int)(fileSize - pos); fileReader.Read(buffer, 0, toSend); int iteration = Environment.TickCount; writer.Write(toSend); conn.Send(buffer, 0, toSend); if (!reader.ReadBoolean()) { Console.WriteLine("Error in transmission"); return; } pos += toSend; ConsoleProgress.Draw(i++, pos, fileSize, ini, Console.WindowWidth / 3); if (bVerbose) { Console.WriteLine(); Console.WriteLine("Current: {0} / s", SizeConverter.ConvertToSizeString(toSend / (Environment.TickCount - iteration) * 1000)); Console.WriteLine("BandwidthMbps {0} mbps.", conn.GetPerformanceInfo().Probe.BandwidthMbps); Console.WriteLine("RoundtripTime {0}.", conn.GetPerformanceInfo().Probe.RoundtripTime); Console.WriteLine("SendMbps {0}.", conn.GetPerformanceInfo().Local.SendMbps); Console.WriteLine("ReceiveMbps {0}.", conn.GetPerformanceInfo().Local.ReceiveMbps); } } } }
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(); }
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); } }
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); } }
public void Connect() { UdtConnection.Send(new byte[] { PACKET_CONNECTION_RQ_TYPE }, 0, 1); Connected = true; }