public static void Run(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: {0} server port", System.AppDomain.CurrentDomain.FriendlyName); return; } Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); socket.Bind(IPAddress.Any, int.Parse(args[0])); socket.Listen(1); while (true) { Udt.Socket client0 = socket.Accept(); Console.WriteLine("First connection accepted from {0}", client0.RemoteEndPoint.ToString()); Udt.Socket client1 = socket.Accept(); Console.WriteLine("Second connection accepted from {0}", client1.RemoteEndPoint.ToString()); IPEndPoint client1EndPoint = client1.RemoteEndPoint; SendAddressTo(client0.RemoteEndPoint, client1); SendAddressTo(client1EndPoint, client0); } }
private Udt.Socket CreateSocket() { Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); socket.ReuseAddress = true; socket.Bind(IPAddress.Any, localPort); return(socket); }
private void StartPeerConnection(PeerInfo peerInfo) { LOGGER.Info("Created new socket for peer connection"); socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); socket.ReuseAddress = true; socket.SetSocketOption(Udt.SocketOptionName.Rendezvous, true); socket.Bind(IPAddress.Any, localPort); LOGGER.Info("Waiting for peer connection"); socket.Connect(peerInfo.Address, peerInfo.Port); LOGGER.Info("peer connected, starting sender and listener threads"); Thread receiveThread = new Thread(() => ListenMessages(socket)); receiveThread.Start(); Thread sendThread = new Thread(() => SendDate(socket)); sendThread.Start(); LOGGER.Info("connection done"); }
public static void Run(string[] args) { if (args.Length < 3) { Console.WriteLine("Usage: {0} client local_port server server_port [client|server]", System.AppDomain.CurrentDomain.FriendlyName); return; } Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); client.ReuseAddress = true; client.Bind(IPAddress.Any, int.Parse(args[0])); IPAddress serverAddress; if (!IPAddress.TryParse(args[1], out serverAddress)) { Console.WriteLine("Error trying to parse {0}", args[1]); return; } client.Connect(serverAddress, int.Parse(args[2])); int peerPort; IPAddress peerAddress; // recv the other peer info using (Udt.NetworkStream st = new Udt.NetworkStream(client, false)) using (BinaryReader reader = new BinaryReader(st)) { int len = reader.ReadInt32(); byte[] addr = reader.ReadBytes(len); peerPort = reader.ReadInt32(); peerAddress = new IPAddress(addr); Console.WriteLine("Received peer address = {0}:{1}", peerAddress, peerPort); } bool bConnected = false; int retry = 0; while (!bConnected) { try { client.Close(); client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); client.ReuseAddress = true; client.SetSocketOption(Udt.SocketOptionName.Rendezvous, true); client.Bind(IPAddress.Any, int.Parse(args[0])); Console.WriteLine("{0} - Trying to connect to {1}:{2}. ", retry++, peerAddress, peerPort); client.Connect(peerAddress, peerPort); Console.WriteLine("Connected successfully to {0}:{1}", peerAddress, peerPort); bConnected = true; } catch (Exception e) { Console.WriteLine(e.Message); } } if (args[3] == "client") { using (Udt.NetworkStream st = new Udt.NetworkStream(client)) using (BinaryReader reader = new BinaryReader(st)) { while (true) { Console.WriteLine(reader.ReadString()); } } } else { using (Udt.NetworkStream st = new Udt.NetworkStream(client)) using (BinaryWriter writer = new BinaryWriter(st)) { int last = Environment.TickCount; while (!Console.KeyAvailable) { if (Environment.TickCount - last < 1000) { continue; } writer.Write(string.Format("[{0}] my local time is {1}", Environment.MachineName, DateTime.Now.ToLongTimeString())); last = Environment.TickCount; TraceInfo traceInfo = client.GetPerformanceInfo(); Console.WriteLine("Bandwith Mbps {0}", traceInfo.Probe.BandwidthMbps); } } } }
public static void Run(string[] args) { if (args.Length < 3) { Console.WriteLine("Usage: {0} client local_port server server_port [client|server]", System.AppDomain.CurrentDomain.FriendlyName); return; } Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); client.ReuseAddress = true; client.Bind(IPAddress.Any, int.Parse(args[0])); IPAddress serverAddress; if (!IPAddress.TryParse(args[1], out serverAddress)) { Console.WriteLine("Error trying to parse {0}", args[1]); return; } client.Connect(serverAddress, int.Parse(args[2])); int peerPort; IPAddress peerAddress; // recv the other peer info using (Udt.NetworkStream st = new Udt.NetworkStream(client, false)) using (BinaryReader reader = new BinaryReader(st)) { int len = reader.ReadInt32(); byte[] addr = reader.ReadBytes(len); peerPort = reader.ReadInt32(); peerAddress = new IPAddress(addr); Console.WriteLine("Received peer address = {0}:{1}", peerAddress, peerPort); } bool bConnected = false; int retry = 0; while (!bConnected) try { client.Close(); client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); client.ReuseAddress = true; client.SetSocketOption(Udt.SocketOptionName.Rendezvous, true); client.Bind(IPAddress.Any, int.Parse(args[0])); Console.WriteLine("{0} - Trying to connect to {1}:{2}. ", retry++, peerAddress, peerPort); client.Connect(peerAddress, peerPort); Console.WriteLine("Connected successfully to {0}:{1}", peerAddress, peerPort); bConnected = true; } catch(Exception e) { Console.WriteLine(e.Message); } if (args[3] == "client") { using (Udt.NetworkStream st = new Udt.NetworkStream(client)) using (BinaryReader reader = new BinaryReader(st)) { while (true) { Console.WriteLine(reader.ReadString()); } } } else { using (Udt.NetworkStream st = new Udt.NetworkStream(client)) using (BinaryWriter writer = new BinaryWriter(st)) { int last = Environment.TickCount; while (!Console.KeyAvailable) { if (Environment.TickCount - last < 1000) continue; writer.Write(string.Format("[{0}] my local time is {1}", Environment.MachineName, DateTime.Now.ToLongTimeString())); last = Environment.TickCount; TraceInfo traceInfo = client.GetPerformanceInfo(); Console.WriteLine("Bandwith Mbps {0}", traceInfo.Probe.BandwidthMbps); } } } }