mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-26 05:32:50 +02:00
30 lines
548 B
Java
30 lines
548 B
Java
|
package net.minestom.server.utils;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.net.ServerSocket;
|
||
|
|
||
|
/**
|
||
|
* Network related utilities.
|
||
|
*/
|
||
|
public class NetworkUtils {
|
||
|
|
||
|
private NetworkUtils() { }
|
||
|
|
||
|
/**
|
||
|
* Gets a free port.
|
||
|
*
|
||
|
* @return the port
|
||
|
* @throws IOException if a port could not be found
|
||
|
*/
|
||
|
public static int getFreePort() throws IOException {
|
||
|
int port;
|
||
|
|
||
|
final ServerSocket socket = new ServerSocket(0);
|
||
|
port = socket.getLocalPort();
|
||
|
|
||
|
socket.close();
|
||
|
|
||
|
return port;
|
||
|
}
|
||
|
}
|