/// <summary> /// Probes the given device (IP Address) /// </summary> /// <param name="deviceAddress">IP Address of the device to probe</param> /// <returns>Returns Device information if it is discovered else returns null</returns> public static DeviceInfo ProbeDevice(IPAddress deviceAddress) { Collection <DeviceInfo> devices = new Collection <DeviceInfo>(); string probeMessage = GetProbeXmlString(); // local machine may have many network interfaces // walk thru each network interface and probe the devices IPHostEntry receiverHost = Dns.GetHostEntry(Dns.GetHostName()); for (int interfaceIndex = 0; interfaceIndex < receiverHost.AddressList.Length; interfaceIndex++) { Collection <string> probeMatches = DiscoveryBase.ProbeDevice(receiverHost.AddressList[interfaceIndex], deviceAddress, probeMessage, PROBE_PORT); ConvertProbeMatchToDeviceInfo(probeMatches, devices); } // since we are probing to a specific device it should get only one device if (devices.Count <= 0) { return(null); } else { return(devices[0]); } }
/// <summary> /// Discovers WS enabled devices in the network. /// Note: This method will not support in threading. /// <param name="ipv4NetworkPrefix">Network prefix to discover only in the given prefix eg: 192.168.201</param> /// <param name="discoverOnIPv6">Discover on IPv6 also</param> /// </summary> /// <returns>Returns the list of devices found in the network</returns> public static Collection <DeviceInfo> Discover(string ipv4NetworkPrefix = null, bool discoverOnIPv6 = true) { Collection <DeviceInfo> devices = new Collection <DeviceInfo>(); // local machine may have many network interfaces // walk thru each network interface and probe the devices IPHostEntry receiverHost = Dns.GetHostEntry(Dns.GetHostName()); string probeMessage = GetProbeXmlString(); Collection <string> probeMatches; for (int interfaceIndex = 0; interfaceIndex < receiverHost.AddressList.Length; interfaceIndex++) { IPAddress clientAddress = receiverHost.AddressList[interfaceIndex]; // if the subnet mask is null discover always if (ipv4NetworkPrefix == null || string.IsNullOrEmpty(ipv4NetworkPrefix)) { // if it is null discover IPAddress ipv4Address = IPAddress.Parse(MULTICAST_PROBE_IPv4); probeMatches = DiscoveryBase.ProbeDevice(clientAddress, ipv4Address, probeMessage, PROBE_PORT); ConvertProbeMatchToDeviceInfo(probeMatches, devices); } else { if (clientAddress.ToString().StartsWith(ipv4NetworkPrefix, StringComparison.InvariantCultureIgnoreCase)) { IPAddress ipv4Address = IPAddress.Parse(MULTICAST_PROBE_IPv4); probeMatches = DiscoveryBase.ProbeDevice(clientAddress, ipv4Address, probeMessage, PROBE_PORT); ConvertProbeMatchToDeviceInfo(probeMatches, devices); } } if (discoverOnIPv6) { // Discovering on IPv6 IPAddress ipv6Address = IPAddress.Parse(MULTICAST_PROBE_IPv6); probeMatches = DiscoveryBase.ProbeDevice(receiverHost.AddressList[interfaceIndex], ipv6Address, probeMessage, PROBE_PORT); ConvertProbeMatchToDeviceInfo(probeMatches, devices); } } return(devices); }