public static int getRandomAvailablePortInRange(int rangeBase, int rangeTop, int protocol) { int numberOfPorts = rangeTop - rangeBase; //do "10 times the numberOfPorts" iterations to select a port number. This will ensure that //each of the ports from given port range get a chance at least once int numberOfRetrys = numberOfPorts * 10; for (int i = 0; i < numberOfRetrys; i++) { int port = rand.nextInt(numberOfPorts + 1) + rangeBase;//add 1 to numberOfPorts so that rangeTop also gets included if (isPortAvailable(port, protocol, getAddress(protocol))) { return port; } } return -1; }
/** * Returns a randomly selected available port in the provided range. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getAvailablePortInRange(int rangeBase, int rangeTop, int protocol) { return getAvailablePortInRange(protocol, getAddress(protocol), rangeBase, rangeTop); }
public static int getRandomAvailablePortInRange(int rangeBase, int rangeTop, int protocol) { int numberOfPorts = rangeTop - rangeBase; //do "5 times the numberOfPorts" iterations to select a port number. This will ensure that //each of the ports from given port range get a chance at least once int numberOfRetrys = numberOfPorts * 5; for (int i = 0; i < numberOfRetrys; i++) { int port = rand.nextInt(numberOfPorts + 1) + rangeBase;//add 1 to numberOfPorts so that rangeTop also gets included if (isPortAvailable(port, protocol, getAddress(protocol))) { return port; } } return -1; }
/** * Returns a randomly selected available port in the provided range. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getAvailablePortInRange(int rangeBase, int rangeTop, int protocol) { return getAvailablePortInRange(protocol, getAddress(protocol), rangeBase, rangeTop); }
public static Keeper getRandomAvailablePortKeeper(int protocol) { return getRandomAvailablePortKeeper(protocol, getAddress(protocol)); }
/** * Returns a randomly selected available port in the range 5001 to * 32767. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePort(int protocol) { return getRandomAvailablePort(protocol, getAddress(protocol)); }
/** * Returns a randomly selected available port in the range 5001 to * 32767 that satisfies a modulus * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePortWithMod(int protocol,int mod) { return getRandomAvailablePortWithMod(protocol, getAddress(protocol),mod); }
public static Keeper getRandomAvailablePortKeeper(int protocol) { return getRandomAvailablePortKeeper(protocol, getAddress(protocol)); }
/** * Returns a randomly selected available port in the range 5001 to * 32767. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePort(int protocol) { return getRandomAvailablePort(protocol, getAddress(protocol)); } public static Keeper getRandomAvailablePortKeeper(int protocol) {
/** * Returns a randomly selected available port in the range 5001 to * 32767 that satisfies a modulus * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePortWithMod(int protocol,int mod) { return getRandomAvailablePortWithMod(protocol, getAddress(protocol),mod); }
/** * Returns whether or not the given port on the local host is * available (that is, unused). * * @param port * The port to check * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static boolean isPortAvailable(final int port, int protocol) { return isPortAvailable(port, protocol, getAddress(protocol)); }
/** * Returns whether or not the given port on the local host is * available (that is, unused). * * @param port * The port to check * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #MULTICAST}). * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static boolean isPortAvailable(final int port, int protocol) { return isPortAvailable(port, protocol, getAddress(protocol)); }