Rom*_*man 188 java sockets port networking
我想启动一个监听端口的服务器.我可以明确指定端口,它可以工作.但我想以自动方式找到一个端口.在这方面,我有两个问题.
我应该在哪个端口号范围内搜索?(我使用了端口12345,12346和12347,它很好).
如何确定某个端口是否未被其他软件占用?
Gra*_*mbe 264
如果您不介意使用的端口,请为ServerSocket构造函数指定一个0的端口,它将侦听任何空闲端口.
ServerSocket s = new ServerSocket(0);
System.out.println("listening on port: " + s.getLocalPort());
Run Code Online (Sandbox Code Playgroud)
如果你想使用一组特定的端口,那么最简单的方法就是迭代它们直到一个工作.像这样的东西:
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");
}
Run Code Online (Sandbox Code Playgroud)
可以像这样使用:
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");
}
Run Code Online (Sandbox Code Playgroud)
And*_*avu 31
从Java 1.7开始,您可以使用try-with-resources,如下所示:
private Integer findRandomOpenPortOnAllLocalInterfaces() throws IOException {
try (
ServerSocket socket = new ServerSocket(0);
) {
return socket.getLocalPort();
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要在特定接口上查找开放端口,请检查ServerSocket文档以获取替代构造函数.
警告:使用此方法返回的端口号的任何代码都会受到竞争条件的影响 - 在关闭ServerSocket实例后,不同的进程/线程可能会立即绑定到同一端口.
Ser*_*kyi 21
如果您需要使用范围:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*aev 20
如果您使用Spring,可以尝试http://docs.spring.io/spring/docs/4.0.5.RELEASE/javadoc-api/org/springframework/util/SocketUtils.html#findAvailableTcpPort--
小智 11
Eclipse SDK包含一个类SocketUtil,它可以满足您的需求.您可以查看git 源代码.
请参阅http://java.sun.com/j2se/1.4.2/docs/api/java/net/ServerSocket.html#ServerSocket%28int%29
创建绑定到指定端口的服务器套接字.端口0在任何空闲端口上创建套接字.
这适用于Java 6
ServerSocket serverSocket = new ServerSocket(0);
System.out.println("listening on port " + serverSocket.getLocalPort());
Run Code Online (Sandbox Code Playgroud)
如果您的服务器启动,则表明该套接字未被使用。
编辑
就像是:
ServerSocket s = null ;
try {
s = new ServerSocket( 0 );
} catch( IOException ioe ){
for( int i = START; i < END ; i++ ) try {
s = new ServerSocket( i );
} catch( IOException ioe ){}
}
// At this point if s is null we are helpless
if( s == null ) {
throw new IOException(
Strings.format("Unable to open server in port range(%d-%d)",START,END));
}
Run Code Online (Sandbox Code Playgroud)
我最近发布了一个小型库,它考虑了测试的目的。Maven依赖项是:
<dependency>
<groupId>me.alexpanov</groupId>
<artifactId>free-port-finder</artifactId>
<version>1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
安装后,可以通过以下方式获取免费端口号:
int port = FreePortFinder.findFreeLocalPort();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
165577 次 |
| 最近记录: |