/** * Returns a randomly selected available port in the provided range. * * @param protocol The protocol to check (either {@link #SOCKET} or {@link #MULTICAST}). * @param addr the bind-address or mcast address to use * * @throws IllegalArgumentException <code>protocol</code> is unknown */ public static int getAvailablePortInRange(int protocol, InetAddress addr, int rangeBase, int rangeTop) { for (int port = rangeBase; port <= rangeTop; port++) { if (isPortAvailable(port, protocol, addr)) { 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)); }
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 range 5001 to 32767 that satisfies a modulus * and the provided protocol * * @param protocol The protocol to check (either {@link #SOCKET} or {@link #MULTICAST}). * @param addr the bind-address or mcast address to use * * @throws IllegalArgumentException <code>protocol</code> is unknown */ public static int getRandomAvailablePortWithMod(int protocol, InetAddress addr, int mod) { while (true) { int port = getRandomWildcardBindPortNumber(); if (isPortAvailable(port, protocol, addr) && (port % mod) == 0) { return port; } } }
/** * Asserts that the specified port is available on the specified network interface, indicated by * it's assigned IP address, on this local system. * * @param bindAddress an InetAddress indicating the bounded network interface to determine whether * the service port is available or not. * @param port an integer indicating the network port to listen for client network requests. * @throws BindException if the network address and port are not available. Address defaults to * localhost (or all network interfaces on the local system) if null. * @see org.apache.geode.internal.AvailablePort */ protected static void assertPortAvailable(final InetAddress bindAddress, final int port) throws BindException { if (!AvailablePort.isPortAvailable(port, AvailablePort.SOCKET, bindAddress)) { throw new BindException( String.format("Network is unreachable; port (%1$d) is not available on %2$s.", port, bindAddress != null ? bindAddress.getCanonicalHostName() : "localhost")); } }
/** * Returns a randomly selected available port in the range 5001 to 32767. * * @param protocol The protocol to check (either {@link #SOCKET} or {@link #MULTICAST}). * @param addr the bind-address or mcast address to use * @param useMembershipPortRange use true if the port will be used for membership * * @throws IllegalArgumentException <code>protocol</code> is unknown */ public static int getRandomAvailablePort(int protocol, InetAddress addr, boolean useMembershipPortRange) { while (true) { int port = getRandomWildcardBindPortNumber(useMembershipPortRange); if (isPortAvailable(port, protocol, addr)) { // don't return the products default multicast port if (!(protocol == MULTICAST && port == DistributionConfig.DEFAULT_MCAST_PORT)) { return port; } } } }
/** * 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; } } } }
out.println("\nPort " + port + " is " + (isPortAvailable(port, protocol, addr) ? "" : "not ") + "available for a " + protocolString + " connection\n");
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; } }