Skip to content

Commit

Permalink
NetworkUtils should be able to hold a free port for later usage #1109 (
Browse files Browse the repository at this point in the history
…#1111)

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

(cherry picked from commit b4282c8)

* #1109 address PR review feedback
  • Loading branch information
ffang committed Mar 18, 2021
1 parent b33d783 commit 0350057
Showing 1 changed file with 12 additions and 5 deletions.
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;
public static final int DEFAULT_STARTING_PORT = 49152;
public static int freeStartingPort = DEFAULT_STARTING_PORT;
private static String hostname;

private static final Logger LOG = LoggerFactory.getLogger(NetworkUtils.class);

private NetworkUtils() {
Expand All @@ -44,18 +45,18 @@ public static int getFreePort() {
}

public static int getFreePort(String host) {
return getFreePort(host, DEFAULT_STARTING_PORT, DEFAULT_ENDING_PORT);
return getFreePort(host, freeStartingPort, DEFAULT_ENDING_PORT);
}

public static int getFreePort(String host, Protocol protocol) {
return getFreePort(host, DEFAULT_STARTING_PORT, DEFAULT_ENDING_PORT, protocol);
return getFreePort(host, freeStartingPort, DEFAULT_ENDING_PORT, protocol);
}

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,12 +76,18 @@ public static boolean checkPort(String host, int port, Protocol protocol) {
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(host, port), 1);
ss.getLocalPort();
if (port == freeStartingPort) {
freeStartingPort++;
}
return true;
} catch (IOException e) {
return false;
}
case UDP:
(new DatagramSocket(new InetSocketAddress(host, port))).close();
if (port == freeStartingPort) {
freeStartingPort++;
}
return true;
default:
return false;
Expand Down

0 comments on commit 0350057

Please sign in to comment.