/** * Returns a randomly selected available port in the provided range. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * @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 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; }
public void testIsPortAvailable() throws IOException { ServerSocket socket = new ServerSocket(); int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); socket.bind(new InetSocketAddress(InetAddressUtil.LOOPBACK, port)); try { Assert.assertFalse(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET, InetAddress.getByName(InetAddressUtil.LOOPBACK_ADDRESS))); //Get local host will return the hostname for the server, so this should succeed, since we're bound to the loopback address only. Assert.assertTrue(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET, InetAddress.getLocalHost())); //This should test all interfaces. Assert.assertFalse(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET)); } finally { socket.close(); } }
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; }
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 * #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)); }
public void testWildcardAddressBound() throws IOException { String osName = System.getProperty("os.name"); if(osName != null && osName.startsWith("Windows")) { //AvailablePort is useless on windows because it uses //setReuseAddr. Do nothing on windows. See bug #39368 return; } ServerSocket socket = new ServerSocket(); int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); socket.bind(new InetSocketAddress((InetAddress)null, port)); try { Assert.assertFalse(AvailablePort.isPortAvailable(port, AvailablePort.SOCKET)); } finally { socket.close(); } } }
/** * 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 com.gemstone.gemfire.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"))); } }
/** * 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 com.gemstone.gemfire.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 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; } } }
/** * Returns a randomly selected available port in the range 5001 to * 32767. * * @param protocol * The protocol to check (either {@link #SOCKET} or {@link * #JGROUPS}). * @param addr the bind-address or mcast address to use * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePort(int protocol, InetAddress addr) { int maxTries = 10000; while (maxTries-- > 0) { int port = getRandomWildcardBindPortNumber(); if (isPortAvailable(port, protocol, addr)) { return port; } } throw new IllegalStateException("Cannot find an available port for protocol=" + protocol + " address=" + addr); } public static Keeper getRandomAvailablePortKeeper(int protocol, InetAddress addr) {
/** * 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 * * @throws IllegalArgumentException * <code>protocol</code> is unknown */ public static int getRandomAvailablePort(int protocol, InetAddress addr) { while (true) { int port = getRandomWildcardBindPortNumber(); if (isPortAvailable(port, protocol, addr)) { // don't return the products default multicast port if ( !(protocol == MULTICAST && port == DistributionConfig.DEFAULT_MCAST_PORT) ){ return port; } } } } public static Keeper getRandomAvailablePortKeeper(int protocol, InetAddress addr) {
/** * 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 * #JGROUPS}). * @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) { int maxTries = 10000; while (maxTries-- > 0) { int port = getRandomWildcardBindPortNumber(); if (isPortAvailable(port, protocol, addr) && (port % mod)==0) { return port; } } throw new IllegalStateException("Cannot find an available port for protocol=" + protocol + " address=" + addr + " mod=" + mod); }
(isPortAvailable(port, protocol, addr) ? "" : "not ") + "available for a " + protocolString + " connection\n");
int port = PORT; while(attempts<100) { if(AvailablePort.isPortAvailable(port,AvailablePort.SOCKET)) { try { server = new Server(port);
assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET)); assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
assertTrue(AvailablePort.isPortAvailable(freeTCPPort, AvailablePort.SOCKET)); assertTrue(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
assertTrue(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET)); assertTrue(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
this.serverPort, 50, null, getLogWriter().convertToLogWriterI18n(), -1); assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET)); assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));