Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkUtils should be able to hold a free port for later usage #1109 #1111

Merged
merged 2 commits into from Mar 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -29,10 +29,11 @@
import org.slf4j.LoggerFactory;

public final class NetworkUtils {
public static final int DEFAULT_STARTING_PORT = 49152;

public static final int DEFAULT_ENDING_PORT = 65535;
private static String hostname;

public static int DEFAULT_STARTING_PORT = 49152;
ffang marked this conversation as resolved.
Show resolved Hide resolved
private static final Logger LOG = LoggerFactory.getLogger(NetworkUtils.class);

private NetworkUtils() {
Expand All @@ -55,7 +56,7 @@ public static int getFreePort(String host, int startingPort, int endingPort) {
return getFreePort(host, startingPort, endingPort, Protocol.TCP);
}

public static int getFreePort(String host, int startingPort, int endingPort, Protocol protocol) {
public static synchronized int getFreePort(String host, int startingPort, int endingPort, Protocol protocol) {
int freePort = 0;
for (int i = startingPort; i <= endingPort; i++) {
boolean found = checkPort(host, i, protocol);
Expand All @@ -75,6 +76,7 @@ public static boolean checkPort(String host, int port, Protocol protocol) {
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(host, port), 1);
ss.getLocalPort();
DEFAULT_STARTING_PORT++;
return true;
} catch (IOException e) {
return false;
Expand Down