|
|
@ -36,6 +36,8 @@ import java.net.ServerSocket;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
@ -173,28 +175,27 @@ public class TransportUtils {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static synchronized int getAvailablePort(int randomAttempts) {
|
|
|
|
public static synchronized int getAvailablePort(int randomAttempts) {
|
|
|
|
ArrayList<Integer> failedPorts = new ArrayList<Integer>(randomAttempts);
|
|
|
|
ArrayList<Integer> failedPorts = new ArrayList<Integer>(randomAttempts);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Random randomNum = new Random();
|
|
|
|
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
|
|
|
int randomPort = MAX_PORT_NUMBER;
|
|
|
|
int randomPort = MAX_PORT_NUMBER;
|
|
|
|
|
|
|
|
|
|
|
|
while (randomAttempts > 0) {
|
|
|
|
while (randomAttempts > 0) {
|
|
|
|
randomPort = randomNum.nextInt(MAX_PORT_NUMBER - MIN_PORT_NUMBER) + MIN_PORT_NUMBER;
|
|
|
|
randomPort = secureRandom.nextInt(MAX_PORT_NUMBER - MIN_PORT_NUMBER) + MIN_PORT_NUMBER;
|
|
|
|
|
|
|
|
|
|
|
|
if (checkIfPortAvailable(randomPort)) {
|
|
|
|
if (checkIfPortAvailable(randomPort)) {
|
|
|
|
return randomPort;
|
|
|
|
return randomPort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
failedPorts.add(randomPort);
|
|
|
|
failedPorts.add(randomPort);
|
|
|
|
randomAttempts--;
|
|
|
|
randomAttempts--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
randomPort = MAX_PORT_NUMBER;
|
|
|
|
randomPort = MAX_PORT_NUMBER;
|
|
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
while (true) {
|
|
|
|
if (!failedPorts.contains(randomPort) && checkIfPortAvailable(randomPort)) {
|
|
|
|
if (!failedPorts.contains(randomPort) && checkIfPortAvailable(randomPort)) {
|
|
|
|
return randomPort;
|
|
|
|
return randomPort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
randomPort--;
|
|
|
|
randomPort--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
|
|
|
|
throw new RuntimeException("SHA1PRNG algorithm could not be found.");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|