All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polyglotted.applauncher.util.Ports Maven / Gradle / Ivy

The newest version!
package io.polyglotted.applauncher.util;

import lombok.SneakyThrows;

import java.net.ServerSocket;
import java.util.Optional;

public abstract class Ports {

    public static int randomPort() {
        return allocate(Optional.empty());
    }

    @SneakyThrows
    public static int allocate(Optional port) {
        try (ServerSocket socket = new ServerSocket(port.orElse(0))) {
            socket.setReuseAddress(true);
            return socket.getLocalPort();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy