From f800bfeec454c2ddc34fd0fa80e0dee84edf6f6f Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Sat, 17 Jan 2026 00:00:43 +0100 Subject: [PATCH] User manually puts in the port --- server/src/main/java/server/PortChecker.java | 21 +++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/server/PortChecker.java b/server/src/main/java/server/PortChecker.java index c0c564a..ebf9038 100644 --- a/server/src/main/java/server/PortChecker.java +++ b/server/src/main/java/server/PortChecker.java @@ -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,13 +14,24 @@ 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"); - } +} /** * Checks whether a port is actually available.