Skip to content

Commit

Permalink
NetworkUtils should be able to hold a free port for later usage apach…
Browse files Browse the repository at this point in the history
  • Loading branch information
ffang committed Mar 17, 2021
1 parent f75b6fe commit b4282c8
Showing 1 changed file with 4 additions and 2 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;
private static String hostname;

public static int DEFAULT_STARTING_PORT = 49152;
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

0 comments on commit b4282c8

Please sign in to comment.