spring-framework / org.springframework.util / SocketUtils

SocketUtils

open class SocketUtils

Simple utility methods for working with network sockets — for example, for finding available ports on localhost.

Within this class, a TCP port refers to a port for a ServerSocket; whereas, a UDP port refers to a port for a DatagramSocket.

Author
Sam Brannen

Author
Ben Hale

Author
Arjen Poutsma

Author
Gunnar Hillert

Author
Gary Russell

Since
4.0

Constructors

<init>

SocketUtils()

Although SocketUtils consists solely of static utility methods, this constructor is intentionally public. Rationale

Static methods from this class may be invoked from within XML configuration files using the Spring Expression Language (SpEL) and the following syntax.

<bean id="bean1" ... p:port="#{T(org.springframework.util.SocketUtils).findAvailableTcpPort(12000)}" />
If this constructor were private, you would be required to supply the fully qualified class name to SpEL's T() function for each usage. Thus, the fact that this constructor is public allows you to reduce boilerplate configuration with SpEL as can be seen in the following example.
<bean id="socketUtils" class="org.springframework.util.SocketUtils" /> <bean id="bean1" ... p:port="#{socketUtils.findAvailableTcpPort(12000)}" /> <bean id="bean2" ... p:port="#{socketUtils.findAvailableTcpPort(30000)}" />

Properties

PORT_RANGE_MAX

static val PORT_RANGE_MAX: Int

The default maximum value for port ranges used when finding an available socket port.

PORT_RANGE_MIN

static val PORT_RANGE_MIN: Int

The default minimum value for port ranges used when finding an available socket port.

Functions

findAvailableTcpPort

open static fun findAvailableTcpPort(): Int

Find an available TCP port randomly selected from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].

open static fun findAvailableTcpPort(minPort: Int): Int

Find an available TCP port randomly selected from the range [minPort, {@value #PORT_RANGE_MAX}].

open static fun findAvailableTcpPort(minPort: Int, maxPort: Int): Int

Find an available TCP port randomly selected from the range [minPort, maxPort].

findAvailableTcpPorts

open static fun findAvailableTcpPorts(numRequested: Int): SortedSet<Int>

Find the requested number of available TCP ports, each randomly selected from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].

open static fun findAvailableTcpPorts(numRequested: Int, minPort: Int, maxPort: Int): SortedSet<Int>

Find the requested number of available TCP ports, each randomly selected from the range [minPort, maxPort].

findAvailableUdpPort

open static fun findAvailableUdpPort(): Int

Find an available UDP port randomly selected from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].

open static fun findAvailableUdpPort(minPort: Int): Int

Find an available UDP port randomly selected from the range [minPort, {@value #PORT_RANGE_MAX}].

open static fun findAvailableUdpPort(minPort: Int, maxPort: Int): Int

Find an available UDP port randomly selected from the range [minPort, maxPort].

findAvailableUdpPorts

open static fun findAvailableUdpPorts(numRequested: Int): SortedSet<Int>

Find the requested number of available UDP ports, each randomly selected from the range [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].

open static fun findAvailableUdpPorts(numRequested: Int, minPort: Int, maxPort: Int): SortedSet<Int>

Find the requested number of available UDP ports, each randomly selected from the range [minPort, maxPort].