public virtual void discover() { try { igd = new IGD(); ListenerThread listener = new ListenerThread(this, igd); listener.Daemon = true; listener.Name = "UPnP Discovery Listener"; listener.Start(); while (!listener.Ready) { Utilities.sleep(100); } foreach (string device in deviceList) { string discoveryRequest = string.Format("M-SEARCH * HTTP/1.1\r\nHOST: {0}:{1:D}\r\nST: {2}\r\nMAN: \"ssdp:discover\"\r\nMX: {3:D}\r\n\r\n", multicastIp, discoveryPort, device, discoveryTimeoutMillis / 1000); IEnumerator <NetworkInterface> networkInterfaces = NetworkInterface.NetworkInterfaces; while (networkInterfaces.MoveNext()) { NetworkInterface networkInterface = networkInterfaces.Current; if (networkInterface.Up && networkInterface.supportsMulticast()) { for (IEnumerator <InetAddress> addresses = networkInterface.InetAddresses; addresses.MoveNext();) { InetAddress address = addresses.Current; if (address is Inet4Address && !address.LoopbackAddress) { MulticastSocket socket = new MulticastSocket(new InetSocketAddress(address, discoverySearchPort)); InetSocketAddress socketAddress = new InetSocketAddress(multicastIp, discoveryPort); DatagramPacket packet = new DatagramPacket(discoveryRequest.GetBytes(), discoveryRequest.Length, socketAddress); socket.send(packet); socket.disconnect(); socket.close(); } } } } } for (int i = 0; i < discoveryTimeoutMillis / 10; i++) { if (listener.Done) { break; } Utilities.sleep(10, 0); } listener.Done = true; while (!listener.Ready) { Utilities.sleep(100); } } catch (IOException e) { Console.WriteLine("discover", e); } }
public ListenerThread(UPnP upnp, IGD igd) { this.upnp = upnp; this.igd = igd; }