public void Accept() { ManualResetEvent acceptedEvent = 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()) { acceptedEvent.Set(); } } }); using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { client.Connect(IPAddress.Loopback, port); acceptedEvent.WaitOne(); } serverTask.Wait(); }
public void BasicRoundTrip() { var serializer = new JsonCommonSerializer(); var port = new Random().Next(6000, 60000); var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); listener.Bind(IPAddress.Loopback, port); listener.Listen(100); var serverTransport = listener.GenerateTransportSource(true); var serverRouter = new DefaultMessageRouter(serverTransport, serializer); serverRouter.AddService <IMyService>(new MyService()); var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); client.Connect(IPAddress.Loopback, port); var clientTransport = client.GenerateTransportSource(false); var clientRouter = new DefaultMessageRouter(clientTransport, serializer); var proxy = clientRouter.AddInterface <IMyService>(); var result = proxy.Add(3, 4).Result; Assert.Equal(7, result); clientRouter.Dispose(); clientTransport.Dispose(); client.Dispose(); serverRouter.Dispose(); serverTransport.Dispose(); listener.Dispose(); }
public void Wait_for_accept() { using (Udt.SocketPoller poller = new Udt.SocketPoller()) using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { socket.Bind(IPAddress.Loopback, 0); socket.Listen(100); ManualResetEvent doneEvent = new ManualResetEvent(false); poller.AddSocket(socket); Task.Factory.StartNew(() => { using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { client.Connect(IPAddress.Loopback, socket.LocalEndPoint.Port); doneEvent.WaitOne(1000); } }); Assert.IsTrue(poller.Wait(TimeSpan.FromSeconds(1))); CollectionAssert.AreEqual(new[] { socket }, poller.WriteSockets); CollectionAssert.AreEqual(new[] { socket }, poller.ReadSockets); Udt.Socket acceptedSocket = socket.Accept(); acceptedSocket.Dispose(); doneEvent.Set(); Assert.IsTrue(poller.Wait(TimeSpan.Zero)); CollectionAssert.AreEqual(new[] { socket }, poller.WriteSockets); CollectionAssert.IsEmpty(poller.ReadSockets); } }
public void BasicRoundTrip() { var serializer = new JsonCommonSerializer(); var port = new Random().Next(6000, 60000); var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); listener.Bind(IPAddress.Loopback, port); listener.Listen(100); var serverTransport = listener.GenerateTransportSource(true); var serverRouter = new DefaultMessageRouter(serverTransport, serializer); serverRouter.AddService<IMyService>(new MyService()); var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); client.Connect(IPAddress.Loopback, port); var clientTransport = client.GenerateTransportSource(false); var clientRouter = new DefaultMessageRouter(clientTransport, serializer); var proxy = clientRouter.AddInterface<IMyService>(); var result = proxy.Add(3, 4).Result; Assert.Equal(7, result); clientRouter.Dispose(); clientTransport.Dispose(); client.Dispose(); serverRouter.Dispose(); serverTransport.Dispose(); listener.Dispose(); }
public void Accept() { ManualResetEvent acceptedEvent = 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()) { acceptedEvent.Set(); } } }); using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { client.Connect(IPAddress.Loopback, port); acceptedEvent.WaitOne(); } serverTask.Wait(); }
public void Listen() { int port = _portNum++; using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { socket.Bind(IPAddress.Any, port); socket.Listen(1); // What condition to assert here? } }
public void Listen__NotBound() { using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { Udt.SocketException error = Assert.Throws <Udt.SocketException>(() => { socket.Listen(1); }); Assert.AreEqual(Udt.SocketError.UnboundSocket, error.SocketErrorCode); } }
public void Listen__InvalidArgs() { using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { ArgumentOutOfRangeException error = Assert.Throws <ArgumentOutOfRangeException>(() => { socket.Listen(0); }); Assert.AreEqual("backlog", error.ParamName); Assert.AreEqual(0, error.ActualValue); } }
public IPEndPoint WaitForSync(PeerInfo peer, String syncId, List <SyncType> syncTypes = null) { var syncer = new Syncer(syncId, syncTypes); var activeIp = syncer.WaitForSyncFromPeer(peer, 60000, _udpClient); ListeningSocket = SetupUdtSocket(); ListeningSocket.Bind(_udpClient.Client); ListeningSocket.Listen(10); Udt.Socket udtClient = ListeningSocket.Accept(); UdtConnection = udtClient; UdtConnection.BlockingReceive = true; Logger.Debug("Successfully completed incoming tunnel with " + activeIp + "-" + syncId); return(activeIp); }
public void Remove_socket() { using (Udt.SocketPoller poller = new Udt.SocketPoller()) using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { socket.Bind(IPAddress.Loopback, 0); socket.Listen(100); ManualResetEvent doneEvent = new ManualResetEvent(false); poller.AddSocket(socket); Assert.IsFalse(poller.Wait(TimeSpan.Zero)); poller.RemoveSocket(socket); Assert.Throws <InvalidOperationException>(() => poller.Wait(TimeSpan.Zero)); } }
public void Remove_socket() { using (Udt.SocketPoller poller = new Udt.SocketPoller()) using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { socket.Bind(IPAddress.Loopback, 0); socket.Listen(100); ManualResetEvent doneEvent = new ManualResetEvent(false); poller.AddSocket(socket); Assert.IsFalse(poller.Wait(TimeSpan.Zero)); poller.RemoveSocket(socket); Assert.Throws<InvalidOperationException>(() => poller.Wait(TimeSpan.Zero)); } }
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 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 Get_State() { 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); Assert.AreEqual(Udt.SocketState.Open, server.State); server.Listen(1); Assert.AreEqual(Udt.SocketState.Listening, server.State); using (Udt.Socket accept = server.Accept()) { Assert.AreEqual(Udt.SocketState.Open, server.State); serverDoneEvent.Set(); Assert.IsTrue(clientDoneEvent.WaitOne(1000)); } } }); using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { Assert.AreEqual(Udt.SocketState.Initial, socket.State); Assert.AreEqual(Udt.SocketState.Initial, socket.GetSocketOption(Udt.SocketOptionName.State)); AssertReadOnly(socket, Udt.SocketOptionName.State); socket.Connect(IPAddress.Loopback, port); Assert.AreEqual(Udt.SocketState.Connected, socket.State); serverDoneEvent.WaitOne(1000); clientDoneEvent.Set(); socket.Close(); Assert.AreEqual(Udt.SocketState.Closed, socket.State); Assert.AreEqual(Udt.SocketState.Closed, socket.GetSocketOption(Udt.SocketOptionName.State)); } }
private void ListenPeers() { socket.Listen(1); while (started) { LOGGER.Info("waiting for first client connection"); Udt.Socket client1 = socket.Accept(); IPEndPoint client1Endpoint = client1.RemoteEndPoint; LOGGER.Info("first client connected. IP:" + client1Endpoint.ToString()); Udt.Socket client2 = socket.Accept(); IPEndPoint client2Endpoint = client2.RemoteEndPoint; LOGGER.Info("second client connected. IP:" + client2Endpoint.ToString()); LOGGER.Info("sending client1 endpoint to client 2"); SendAddressTo(client1Endpoint, client2); LOGGER.Info("sending client2 endpoint to client 1"); SendAddressTo(client2Endpoint, client1); LOGGER.Info("PEERS CONNECTED"); } }
public void Benchmark() { //var serializerSource = new Newtonsoft.Json.JsonSerializer(); var serializer = new ProtobufCommonSerializer();//new JsonCommonSerializer(serializerSource); // var port = new Random().Next(6000, 60000); var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); listener.Bind(IPAddress.Loopback, port); listener.Listen(100); var serverTransport = listener.GenerateTransportSource(true); var serverRouter = new DefaultMessageRouter(serverTransport, serializer); serverRouter.AddService<ISumService>(new SumService()); var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); client.Connect(IPAddress.Loopback, port); var clientTransport = client.GenerateTransportSource(false); var clientRouter = new DefaultMessageRouter(clientTransport, serializer); var proxy = clientRouter.AddInterface<ISumService>(); const int randCnt = 100; var rand = new Random(42); var randoms = new int[randCnt]; for (int i = 0; i < randCnt; i++) randoms[i] = rand.Next(10000000, 20000000); var sw = new Stopwatch(); long timeFromClient = 0, timeToClient = 0; const int cnt = 1000; for (int j = 0; j < cnt; j++) { sw.Start(); var sum = proxy.Sum(randoms).Result; sw.Stop(); Assert.Equal(randoms.Sum(), sum); for (int i = 0; i < randCnt; i++) randoms[i] = rand.Next(10000000, 20000000); var times = proxy.TimeDiff(Stopwatch.GetTimestamp()).Result; timeFromClient += times.Item1; timeToClient += Stopwatch.GetTimestamp() - times.Item2; } _testOutputHelper.WriteLine("Completed {0} sum passes in {1}ms", cnt, sw.ElapsedMilliseconds); _testOutputHelper.WriteLine("Client to server latency: {0}us", timeFromClient / cnt / 10); _testOutputHelper.WriteLine("Server to client latency: {0}us", timeToClient / cnt / 10); sw.Reset(); var tree = new SumServiceTree(); SumServiceTree.FillTree(tree, rand, 2); _testOutputHelper.WriteLine("Starting large message transfer."); sw.Start(); var result = proxy.Increment(tree).Result; sw.Stop(); Assert.Equal(tree.Leaf + 1, result.Leaf); _testOutputHelper.WriteLine("Completed large transfer in {0}ms", sw.Elapsed.TotalMilliseconds); clientRouter.Dispose(); clientTransport.Dispose(); client.Dispose(); serverRouter.Dispose(); serverTransport.Dispose(); listener.Dispose(); }
public void Listen() { int port = _portNum++; using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { socket.Bind(IPAddress.Any, port); socket.Listen(1); // What condition to assert here? } }
public void Listen__InvalidArgs() { using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { ArgumentOutOfRangeException error = Assert.Throws<ArgumentOutOfRangeException>(() => { socket.Listen(0); }); Assert.AreEqual("backlog", error.ParamName); Assert.AreEqual(0, error.ActualValue); } }
public void Listen__NotBound() { using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { Udt.SocketException error = Assert.Throws<Udt.SocketException>(() => { socket.Listen(1); }); Assert.AreEqual(Udt.SocketError.UnboundSocket, error.SocketErrorCode); } }
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(); }
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 Wait_for_accept() { using (Udt.SocketPoller poller = new Udt.SocketPoller()) using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { socket.Bind(IPAddress.Loopback, 0); socket.Listen(100); ManualResetEvent doneEvent = new ManualResetEvent(false); poller.AddSocket(socket); Task.Factory.StartNew(() => { using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { client.Connect(IPAddress.Loopback, socket.LocalEndPoint.Port); doneEvent.WaitOne(1000); } }); Assert.IsTrue(poller.Wait(TimeSpan.FromSeconds(1))); CollectionAssert.AreEqual(new[] { socket }, poller.WriteSockets); CollectionAssert.AreEqual(new[] { socket }, poller.ReadSockets); Udt.Socket acceptedSocket = socket.Accept(); acceptedSocket.Dispose(); doneEvent.Set(); Assert.IsTrue(poller.Wait(TimeSpan.Zero)); CollectionAssert.AreEqual(new[] { socket }, poller.WriteSockets); CollectionAssert.IsEmpty(poller.ReadSockets); } }
public IPEndPoint WaitForSync(PeerInfo peer, String syncId, List<SyncType> syncTypes = null) { var syncer = new Syncer(syncId, syncTypes); var activeIp = syncer.WaitForSyncFromPeer(peer, 60000, _udpClient); ListeningSocket = SetupUdtSocket(); ListeningSocket.Bind(_udpClient.Client); ListeningSocket.Listen(10); Udt.Socket udtClient = ListeningSocket.Accept(); UdtConnection = udtClient; UdtConnection.BlockingReceive = true; Logger.Debug("Successfully completed incoming tunnel with " + activeIp + "-" + syncId); return activeIp; }
public void Benchmark() { //var serializerSource = new Newtonsoft.Json.JsonSerializer(); var serializer = new ProtobufCommonSerializer(); //new JsonCommonSerializer(serializerSource); // var port = new Random().Next(6000, 60000); var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); listener.Bind(IPAddress.Loopback, port); listener.Listen(100); var serverTransport = listener.GenerateTransportSource(true); var serverRouter = new DefaultMessageRouter(serverTransport, serializer); serverRouter.AddService <ISumService>(new SumService()); var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram); client.Connect(IPAddress.Loopback, port); var clientTransport = client.GenerateTransportSource(false); var clientRouter = new DefaultMessageRouter(clientTransport, serializer); var proxy = clientRouter.AddInterface <ISumService>(); const int randCnt = 100; var rand = new Random(42); var randoms = new int[randCnt]; for (int i = 0; i < randCnt; i++) { randoms[i] = rand.Next(10000000, 20000000); } var sw = new Stopwatch(); long timeFromClient = 0, timeToClient = 0; const int cnt = 1000; for (int j = 0; j < cnt; j++) { sw.Start(); var sum = proxy.Sum(randoms).Result; sw.Stop(); Assert.Equal(randoms.Sum(), sum); for (int i = 0; i < randCnt; i++) { randoms[i] = rand.Next(10000000, 20000000); } var times = proxy.TimeDiff(Stopwatch.GetTimestamp()).Result; timeFromClient += times.Item1; timeToClient += Stopwatch.GetTimestamp() - times.Item2; } _testOutputHelper.WriteLine("Completed {0} sum passes in {1}ms", cnt, sw.ElapsedMilliseconds); _testOutputHelper.WriteLine("Client to server latency: {0}us", timeFromClient / cnt / 10); _testOutputHelper.WriteLine("Server to client latency: {0}us", timeToClient / cnt / 10); sw.Reset(); var tree = new SumServiceTree(); SumServiceTree.FillTree(tree, rand, 2); _testOutputHelper.WriteLine("Starting large message transfer."); sw.Start(); var result = proxy.Increment(tree).Result; sw.Stop(); Assert.Equal(tree.Leaf + 1, result.Leaf); _testOutputHelper.WriteLine("Completed large transfer in {0}ms", sw.Elapsed.TotalMilliseconds); clientRouter.Dispose(); clientTransport.Dispose(); client.Dispose(); serverRouter.Dispose(); serverTransport.Dispose(); listener.Dispose(); }
internal static void RecieveFiles(NetIncomingMessage msg) { Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); socket.Bind(IPAddress.Any, 10000); socket.Listen(10); Udt.Socket client = socket.Accept(); // Receive the file length, in bytes byte[] buffer = new byte[8]; client.Receive(buffer, 0, sizeof(long)); // Receive the file contents (path is where to store the file) client.ReceiveFile("hello.txt", BitConverter.ToInt64(buffer, 0)); Console.WriteLine("OI!"); }
public void Get_State() { 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); Assert.AreEqual(Udt.SocketState.Open, server.State); server.Listen(1); Assert.AreEqual(Udt.SocketState.Listening, server.State); using (Udt.Socket accept = server.Accept()) { Assert.AreEqual(Udt.SocketState.Open, server.State); serverDoneEvent.Set(); Assert.IsTrue(clientDoneEvent.WaitOne(1000)); } } }); using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { Assert.AreEqual(Udt.SocketState.Initial, socket.State); Assert.AreEqual(Udt.SocketState.Initial, socket.GetSocketOption(Udt.SocketOptionName.State)); AssertReadOnly(socket, Udt.SocketOptionName.State); socket.Connect(IPAddress.Loopback, port); Assert.AreEqual(Udt.SocketState.Connected, socket.State); serverDoneEvent.WaitOne(1000); clientDoneEvent.Set(); socket.Close(); Assert.AreEqual(Udt.SocketState.Closed, socket.State); Assert.AreEqual(Udt.SocketState.Closed, socket.GetSocketOption(Udt.SocketOptionName.State)); } }
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); } }
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 override void Start() { udtSocket.Listen(10); }