class RmiClientDiscovery {

	public static void find_open_ports(String host, int min_port, int max_port,
			PortFoundListener listener, PortScanListener scan_listener) {
		if (host == null) {
			throw new IllegalArgumentException("host == null");
		}
		
		if (listener == null) {
			throw new IllegalArgumentException("listener == null");
		}
		
		if (min_port <= 0) {
			throw new IllegalArgumentException("minPort <= 0");
		}
		
		if (max_port < min_port) {
			throw new IllegalArgumentException("maxPort < minPort");
		}
		
		for (int i = min_port; i <= max_port; i++) {
			try (Socket s = new Socket()) {
				s.connect(new InetSocketAddress(host, i), MAX_WAIT_OPEN_MS);
				if (scan_listener != null) {
					scan_listener.port_scanned(i);
				}
				s.close();
				listener.port_found(i);
			} catch (IOException e) {
				/*
				 * Port is not open.
				 */
				if (scan_listener != null) {
					scan_listener.port_scanned(i);
				}
			}
		}
	}

}