static void SendAddressTo(IPEndPoint endPoint, Udt.Socket socket) { using (Udt.NetworkStream st = new Udt.NetworkStream(socket)) using (BinaryWriter writer = new BinaryWriter(st)) { byte[] addrBytes = endPoint.Address.GetAddressBytes(); writer.Write((int)addrBytes.Length); writer.Write(addrBytes); writer.Write((int)endPoint.Port); } }
private void ListenMessages(Udt.Socket socket) { try { using (Udt.NetworkStream st = new Udt.NetworkStream(socket)) using (BinaryReader reader = new BinaryReader(st)) { while (started) { LOGGER.Info("waiting for new message"); LOGGER.Info("RECEIVED: " + reader.ReadString()); } } } catch (Exception ex) { LOGGER.Error("message listen exception", ex); } LOGGER.Info("listen messages stopped"); }
private void SendDate(Udt.Socket socket) { try { using (Udt.NetworkStream st = new Udt.NetworkStream(socket)) using (BinaryWriter writer = new BinaryWriter(st)) { Thread.Sleep(10000); while (started) { LOGGER.Info("Sending date"); writer.Write(DateTime.Now.ToLongTimeString()); Thread.Sleep(1000); } } } catch (Exception ex) { LOGGER.Error("send date exception", ex); } LOGGER.Info("send date stopped"); }
public PeerInfo ReceivePeerInfo() { PeerInfo peerInfo = null; using (Udt.NetworkStream st = new Udt.NetworkStream(socket, false)) { LOGGER.Info("Waiting peer connection info from signalling server"); byte[] peerInfoBytes = new byte[8]; st.Read(peerInfoBytes, 0, peerInfoBytes.Length); LOGGER.Info("parsing peer connection info"); byte[] peerAddressBytes = new byte[4]; byte[] peerPortBytes = new byte[4]; Array.Copy(peerInfoBytes, 0, peerAddressBytes, 0, peerAddressBytes.Length); Array.Copy(peerInfoBytes, 4, peerPortBytes, 0, peerPortBytes.Length); peerInfo = new PeerInfo(); peerInfo.Address = new IPAddress(peerAddressBytes); peerInfo.Port = BitConverter.ToInt32(peerPortBytes, 0); LOGGER.Info("peer address: " + peerInfo.Address.ToString() + " port: " + peerInfo.Port); } return(peerInfo); }
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); } } } }