class-level Javadoc
for details.@Deprecated public class SocketUtils extends Object
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
.
SocketUtils
was introduced in Spring Framework 4.0, primarily to
assist in writing integration tests which start an external server on an
available random port. However, these utilities make no guarantee about the
subsequent availability of a given port and are therefore unreliable. Instead
of using SocketUtils
to find an available local port for a server, it
is recommended that you rely on a server's ability to start on a random port
that it selects or is assigned by the operating system. To interact with that
server, you should query the server for the port it is currently using.
Modifier and Type | Field and Description |
---|---|
static int |
PORT_RANGE_MAX
Deprecated.
The default maximum value for port ranges used when finding an available
socket port.
|
static int |
PORT_RANGE_MIN
Deprecated.
The default minimum value for port ranges used when finding an available
socket port.
|
Constructor and Description |
---|
SocketUtils()
Deprecated.
Although
SocketUtils consists solely of static utility methods,
this constructor is intentionally public . |
Modifier and Type | Method and Description |
---|---|
static int |
findAvailableTcpPort()
Deprecated.
|
static int |
findAvailableTcpPort(int minPort)
Deprecated.
Find an available TCP port randomly selected from the range
[
minPort , 65535]. |
static int |
findAvailableTcpPort(int minPort,
int maxPort)
Deprecated.
Find an available TCP port randomly selected from the range
[
minPort , maxPort ]. |
static SortedSet<Integer> |
findAvailableTcpPorts(int numRequested)
Deprecated.
|
static SortedSet<Integer> |
findAvailableTcpPorts(int numRequested,
int minPort,
int maxPort)
Deprecated.
Find the requested number of available TCP ports, each randomly selected
from the range [
minPort , maxPort ]. |
static int |
findAvailableUdpPort()
Deprecated.
|
static int |
findAvailableUdpPort(int minPort)
Deprecated.
Find an available UDP port randomly selected from the range
[
minPort , 65535]. |
static int |
findAvailableUdpPort(int minPort,
int maxPort)
Deprecated.
Find an available UDP port randomly selected from the range
[
minPort , maxPort ]. |
static SortedSet<Integer> |
findAvailableUdpPorts(int numRequested)
Deprecated.
|
static SortedSet<Integer> |
findAvailableUdpPorts(int numRequested,
int minPort,
int maxPort)
Deprecated.
Find the requested number of available UDP ports, each randomly selected
from the range [
minPort , maxPort ]. |
public static final int PORT_RANGE_MIN
public static final int PORT_RANGE_MAX
public SocketUtils()
SocketUtils
consists solely of static utility methods,
this constructor is intentionally public
.
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)}" />
public static int findAvailableTcpPort()
IllegalStateException
- if no available port could be foundpublic static int findAvailableTcpPort(int minPort)
minPort
, 65535].minPort
- the minimum port numberIllegalStateException
- if no available port could be foundpublic static int findAvailableTcpPort(int minPort, int maxPort)
minPort
, maxPort
].minPort
- the minimum port numbermaxPort
- the maximum port numberIllegalStateException
- if no available port could be foundpublic static SortedSet<Integer> findAvailableTcpPorts(int numRequested)
numRequested
- the number of available ports to findIllegalStateException
- if the requested number of available ports could not be foundpublic static SortedSet<Integer> findAvailableTcpPorts(int numRequested, int minPort, int maxPort)
minPort
, maxPort
].numRequested
- the number of available ports to findminPort
- the minimum port numbermaxPort
- the maximum port numberIllegalStateException
- if the requested number of available ports could not be foundpublic static int findAvailableUdpPort()
IllegalStateException
- if no available port could be foundpublic static int findAvailableUdpPort(int minPort)
minPort
, 65535].minPort
- the minimum port numberIllegalStateException
- if no available port could be foundpublic static int findAvailableUdpPort(int minPort, int maxPort)
minPort
, maxPort
].minPort
- the minimum port numbermaxPort
- the maximum port numberIllegalStateException
- if no available port could be foundpublic static SortedSet<Integer> findAvailableUdpPorts(int numRequested)
numRequested
- the number of available ports to findIllegalStateException
- if the requested number of available ports could not be foundpublic static SortedSet<Integer> findAvailableUdpPorts(int numRequested, int minPort, int maxPort)
minPort
, maxPort
].numRequested
- the number of available ports to findminPort
- the minimum port numbermaxPort
- the maximum port numberIllegalStateException
- if the requested number of available ports could not be found