/
PortFinder.java
51 lines (45 loc) · 1.17 KB
/
PortFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package newselenium;
import java.io.IOException;
import java.net.ServerSocket;
/**
* Finds an available port on localhost.
*/
public class PortFinder {
// the ports below 1024 are system ports
private static final int MIN_PORT_NUMBER = 1024;
// the ports above 49151 are dynamic and/or private
private static final int MAX_PORT_NUMBER = 49151;
/**
* Finds a free port between {@link #MIN_PORT_NUMBER} and
* {@link #MAX_PORT_NUMBER}.
*
* @return a free port
* @throw RuntimeException if a port could not be found
*/
public static int findFreePort() {
for (int i = MIN_PORT_NUMBER; i <= MAX_PORT_NUMBER; i++) {
if (available(i)) {
return i;
}
}
throw new RuntimeException("Could not find an available port between " + MIN_PORT_NUMBER + " and " + MAX_PORT_NUMBER);
}
private static boolean available(final int port) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
serverSocket.close();
return true;
} catch (final IOException e) {
return false;
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (final IOException e) {
// can never happen
}
}
}
}
}