// TODO: make timeout work correctly public bool connect(string host, int port = -1, int timeout_ms = 5000) { if (port < 0) { port = Constants.DEFAULTPORT; } if (this.clientSocket != null) { this.clientSocket.Close(); this.clientSocket = null; } if (host == null || host.Length == 0) { // auto-search for host port Console.WriteLine("Trying SSDP discovery"); var devices = SSDPDiscovery.ssdpDiscover("utopia/1.1", timeout_ms); Console.WriteLine("Discovered " + devices.Count() + " devices"); if (devices.Any()) { var dev = devices[0]; if (dev.ContainsKey("location")) { host = dev["location"]; port = Constants.DEFAULTPORT; if (host.StartsWith("http://")) { host = host.Substring("http://".Length); } if (host.Contains("/")) { host = host.Substring(0, host.IndexOf("/")); } string[] hostport = host.Split(':'); host = hostport[0]; if (hostport.Length > 1) { Int32.TryParse(hostport[1], out port); } } Console.WriteLine("SSDP discovered: " + host + ":" + port); } } if (host == null || host.Length == 0) { return(false); } this.clientSocket = new TcpClient(host, port); this.networkStream = this.clientSocket.GetStream(); this.clientSocket.ReceiveTimeout = 500; this.clientSocket.NoDelay = true; // disable Nagle? // udp client bound to same local port number to make matching udp/tcp messages easier this.udpClient = new UdpClient(((System.Net.IPEndPoint) this.clientSocket.Client.LocalEndPoint).Port); this.udpClient.Connect(host, port); return(this.clientSocket.Connected); }
public static void Main(string[] argv) { var devices = SSDPDiscovery.ssdpDiscover("ssdp:all", 10000); foreach (var dev in devices) { Console.WriteLine("Device"); foreach (KeyValuePair <string, string> kvp in dev) { Console.WriteLine("{0}:{1}", kvp.Key, kvp.Value); } } }