获取系统空闲端口

端口取值范围

以下搜自互联网

一般用到的是1到65535,其中0不使用,1-1023为系统端口,也叫BSD保留端口;1024-65535为用户端口,又分为: BSD临时端口(1024-5000)和BSD服务器(非特权)端口(5001-65535).

0-1023: BSD保留端口,也叫系统端口,这些端口只有系统特许的进程才能使用;

1024-5000: BSD临时端口,一般的应用程序使用1024到4999来进行通讯;

5001-65535: BSD服务器(非特权)端口,用来给用户自定义端口.

常规方法

首先想到的方法有两种

取[1024, 65536)之间的一个随机数循环取[1024, 65536)进行bind,直到bind成功为止

第一种方法,无法保证随机获取的端口是未占用的

第二种方法,不停的尝试bind,效率很低

使用InetSocketAddress

InetSocketAddress有三种构造函数

InetSocketAddress(int port)InetSocketAddress(InetAddress addr, int port)InetSocketAddress(String hostname, int port)

查看java源码中对第一种构造函数的解释

/**

* Creates a socket address where the IP address is the wildcard address

* and the port number a specified value.

*

* A valid port value is between 0 and 65535.

* A port number of {@code zero} will let the system pick up an

* ephemeral port in a {@code bind} operation.

*

* @param port The port number

* @throws IllegalArgumentException if the port parameter is outside the specified

* range of valid port values.

*/

public InetSocketAddress(int port) {

this(InetAddress.anyLocalAddress(), port);

}

当入参port为0时,由系统自动分配一个临时端口,据此,便可以写出一个工具类,如下

/**

*

* 系统空闲端口

*

*

* 系统空闲端口

*

* use like:

* SysFreePort.custom().getPort();

* SysFreePort.custom().getPortAndFree();

*

*

* @author ManerFan 2015年2月3日

*/

public class SysFreePort {

private static Random random = new Random();

private Socket socket;

/**

*

* 获取系统空闲端口

*

*

* 获取系统空闲端口,并占用该端口资源

*

*

* @throws IOException

*/

public static SysFreePort custom() throws IOException {

return new SysFreePort();

}

private SysFreePort() throws IOException {

socket = new Socket();

InetSocketAddress inetAddress = new InetSocketAddress(0);

socket.bind(inetAddress);

}

/**

*

* 释放端口资源

*

*

* 释放该端口资源

*

*

* @throws IOException

*/

public void freePort() throws IOException {

if (null == socket || socket.isClosed()) {

return;

}

socket.close();

}

/**

*

* 返回端口

*

*

* 返回端口,不释放该端口资源

*

*/

public int getPort() {

if (null == socket || socket.isClosed()) {

return -1;

}

return socket.getLocalPort();

}

/**

*

* 返回端口

*

*

* 返回端口并释放该端口资源

*

*

* @throws IOException

*/

public int getPortAndFree() throws IOException {

if (null == socket || socket.isClosed()) {

return -1;

}

int port = socket.getLocalPort();

socket.close();

return port;

}

/**

*

* 生成随机port

*

*

* 在[start, end)间随机生成一个数字作为port

*

*

* @param start

* @param end

* @return int

*/

public static int random(int start, int end) {

return random.nextInt(Math.abs(end - start)) + start;

}

}

使用SysFreePort port = SysFreePort.custom();获取系统空闲端口并占用(防止其他程序再次占用此端口)

使用port.getPort();返回获取到的空闲端口(不释放端口资源)

使用port.getPortAndFree();返回获取到的空闲端口,并释放该端口资源

使用port.freePort();释放该端口资源