我想启动一个侦听端口的服务器。我可以显式地指定端口,它工作。但我想以自动的方式找到一个端口。在这方面,我有两个问题。
我应该在哪个范围内搜索端口号?(我使用的端口是12345、12346和12347,没有问题)。 我如何知道给定的端口是否被其他软件占用?
我想启动一个侦听端口的服务器。我可以显式地指定端口,它工作。但我想以自动的方式找到一个端口。在这方面,我有两个问题。
我应该在哪个范围内搜索端口号?(我使用的端口是12345、12346和12347,没有问题)。 我如何知道给定的端口是否被其他软件占用?
当前回答
这里有很多使用ServerSocket的答案。我检查了Micronauts实现,他们转而尝试将客户端套接字连接到本地端口,如果失败,他们说端口是开放的。对我来说,这样做的好处是他们不必冒险在测试中使用端口。
它们的代码是这样的:
public static boolean isTcpPortAvailable(int currentPort) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), currentPort), 20);
return false;
} catch (Throwable e) {
return true;
}
}
参考网址:https://github.com/micronaut-projects/micronaut-core/blob/3.4.x/core/src/main/java/io/micronaut/core/io/socket/SocketUtils.java
其他回答
使用'ServerSocket'类,我们可以确定给定的端口是正在使用还是空闲。ServerSocket提供了一个以整数(即端口号)为参数的构造函数 参数并初始化端口上的服务器套接字。如果ServerSocket抛出任何IO异常,那么我们可以假设这个端口已经被使用。
下面的代码片段用于获取所有可用端口。
for (int port = 1; port < 65535; port++) {
try {
ServerSocket socket = new ServerSocket(port);
socket.close();
availablePorts.add(port);
} catch (IOException e) {
}
}
参考链接。
如果您需要在范围内使用:
public int nextFreePort(int from, int to) {
int port = randPort(from, to);
while (true) {
if (isLocalPortFree(port)) {
return port;
} else {
port = ThreadLocalRandom.current().nextInt(from, to);
}
}
}
private boolean isLocalPortFree(int port) {
try {
new ServerSocket(port).close();
return true;
} catch (IOException e) {
return false;
}
}
如果您将0作为端口号传递给ServerSocket的构造函数,它将为您分配一个端口。
如果不介意使用的端口,可以向ServerSocket构造函数指定一个端口0,它将侦听任何空闲端口。
ServerSocket s = new ServerSocket(0);
System.out.println("listening on port: " + s.getLocalPort());
如果您希望使用一组特定的端口,那么最简单的方法可能是遍历它们,直到其中一个可以工作为止。就像这样:
public ServerSocket create(int[] ports) throws IOException {
for (int port : ports) {
try {
return new ServerSocket(port);
} catch (IOException ex) {
continue; // try next port
}
}
// if the program gets here, no port in the range was found
throw new IOException("no free port found");
}
可以这样使用:
try {
ServerSocket s = create(new int[] { 3843, 4584, 4843 });
System.out.println("listening on port: " + s.getLocalPort());
} catch (IOException ex) {
System.err.println("no available ports");
}
你可以让ServerSocket为你找到一个端口,然后关闭它:
private int getFreePort() {
var freePort = 0;
try (ServerSocket s = new ServerSocket(0)) {
freePort = s.getLocalPort();
} catch (Exception e) {
e.printStackTrace();
}
return freePort;
}