User manually puts in the port

This commit is contained in:
Mei Chang van der Werff 2026-01-17 00:00:43 +01:00
commit f800bfeec4

View file

@ -2,10 +2,10 @@ package server;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Scanner;
public class PortChecker {
private static final int defaultPort = 8080;
private static final int lastPort = 8090; // To limit the amount of ports to look for availability
/**
* Finds a free port to launch the program on.
@ -14,12 +14,23 @@ public class PortChecker {
* @throws IOException when no available port is found
*/
public int findFreePort() throws IOException {
for (int port = defaultPort; port <= lastPort; port++) {
if(isPortAvailable(port)){
if(isPortAvailable(defaultPort)){
return defaultPort;
}
System.out.println("Please enter the port number you want to use");
Scanner portScanner = new Scanner(System.in);
while(true){
int port = portScanner.nextInt();
if(port > 0 && port <=65535){ //range of valid ports
isPortAvailable(port);
return port;
}
else {
System.out.println("Try a different port:");
}
}
throw new IOException("No free port found");
}
/**