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 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)); }
/** * 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)); }
/** * 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); }
/** * 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)); }
/** * Get the next available port on this machine. */ private static int getUniquePort(boolean useMembershipPortRange, int protocol) { AtomicInteger targetRange = useMembershipPortRange ? singleton.currentMembershipPort : singleton.currentAvailablePort; int targetBound = useMembershipPortRange ? DEFAULT_MEMBERSHIP_PORT_RANGE[1] : AVAILABLE_PORTS_UPPER_BOUND; while (true) { int uniquePort = targetRange.getAndIncrement(); if (uniquePort > targetBound) { targetRange.set(useMembershipPortRange ? DEFAULT_MEMBERSHIP_PORT_RANGE[0] : AVAILABLE_PORTS_LOWER_BOUND); continue; } if (AvailablePort.isPortAvailable(uniquePort, protocol, getAddress(protocol))) { return uniquePort; } } } }
Keeper testKeeper = AvailablePort.isPortKeepable(uniquePort++, protocol, getAddress(protocol)); if (testKeeper == null) { break;
private boolean tryToStart(int port) { if (!AvailablePort.isPortAvailable(port, AvailablePort.SOCKET, AvailablePort.getAddress(AvailablePort.SOCKET))) { return false; } receiver.setPort(port); receiver.setSocketBufferSize(socketBufferSize); receiver.setMaximumTimeBetweenPings(timeBetPings); if (hostnameForSenders != null && !hostnameForSenders.isEmpty()) { receiver.setHostnameForClients(hostnameForSenders); } receiver.setBindAddress(bindAdd); receiver.setGroups(new String[] {GatewayReceiver.RECEIVER_GROUP}); ((CacheServerImpl) receiver).setGatewayTransportFilter(this.filters); try { receiver.start(); this.port = port; return true; } catch (IOException e) { logger.info("Failed to create server socket on {}[{}]", bindAdd, port); return false; } }