User manually puts in the port
This commit is contained in:
parent
61562d58dc
commit
f800bfeec4
1 changed files with 16 additions and 5 deletions
|
|
@ -2,10 +2,10 @@ package server;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class PortChecker {
|
public class PortChecker {
|
||||||
private static final int defaultPort = 8080;
|
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.
|
* Finds a free port to launch the program on.
|
||||||
|
|
@ -14,13 +14,24 @@ public class PortChecker {
|
||||||
* @throws IOException when no available port is found
|
* @throws IOException when no available port is found
|
||||||
*/
|
*/
|
||||||
public int findFreePort() throws IOException {
|
public int findFreePort() throws IOException {
|
||||||
for (int port = defaultPort; port <= lastPort; port++) {
|
if(isPortAvailable(defaultPort)){
|
||||||
if(isPortAvailable(port)){
|
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;
|
return port;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Try a different port:");
|
||||||
}
|
}
|
||||||
throw new IOException("No free port found");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether a port is actually available.
|
* Checks whether a port is actually available.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue