public class SocketUtils
extends java.lang.Object
localhost
で使用可能なポートを検索する場合。 このクラス内で、TCP ポートは ServerSocket
のポートを参照します。一方、UDP ポートは DatagramSocket
のポートを指します。
修飾子と型 | フィールドと説明 |
---|---|
static int | PORT_RANGE_MAX 使用可能なソケットポートを見つけるときに使用されるポート範囲のデフォルトの最大値。 |
static int | PORT_RANGE_MIN 使用可能なソケットポートを見つけるときに使用されるポート範囲のデフォルトの最小値。 |
コンストラクターと説明 |
---|
SocketUtils() SocketUtils は静的ユーティリティメソッドのみで構成されていますが、このコンストラクターは意図的に public です。 |
修飾子と型 | メソッドと説明 |
---|---|
static int | findAvailableTcpPort() |
static int | findAvailableTcpPort(int minPort) 範囲 [ minPort 、65535 ] からランダムに選択された使用可能な TCP ポートを見つけます。 |
static int | findAvailableTcpPort(int minPort, int maxPort) 範囲 [ minPort 、maxPort ] からランダムに選択された使用可能な TCP ポートを見つけます。 |
static java.util.SortedSet<java.lang.Integer> | findAvailableTcpPorts(int numRequested) |
static java.util.SortedSet<java.lang.Integer> | findAvailableTcpPorts(int numRequested, int minPort, int maxPort) 範囲 [ minPort 、maxPort ] からランダムに選択された、使用可能な TCP ポートのリクエストされた数を見つけます。 |
static int | findAvailableUdpPort() |
static int | findAvailableUdpPort(int minPort) 範囲 [ minPort 、65535 ] からランダムに選択された使用可能な UDP ポートを見つけます。 |
static int | findAvailableUdpPort(int minPort, int maxPort) 範囲 [ minPort 、maxPort ] からランダムに選択された使用可能な UDP ポートを見つけます。 |
static java.util.SortedSet<java.lang.Integer> | findAvailableUdpPorts(int numRequested) |
static java.util.SortedSet<java.lang.Integer> | findAvailableUdpPorts(int numRequested, int minPort, int maxPort) 範囲 [ minPort 、maxPort ] からランダムに選択された、使用可能な UDP ポートのリクエストされた数を見つけます。 |
public static final int PORT_RANGE_MIN
public static final int PORT_RANGE_MAX
public SocketUtils()
SocketUtils
は静的ユーティリティメソッドのみで構成されていますが、このコンストラクターは意図的に public
です。このクラスの静的メソッドは、Spring 式言語(SpEL)と次の構文を使用して、XML 構成ファイル内から呼び出すことができます。
<bean id="bean1" ... p:port="#{T(org.springframework.util.SocketUtils).findAvailableTcpPort(12000)}" />
このコンストラクターが private
の場合、使用箇所ごとに完全修飾クラス名を SpEL の T()
関数に指定する必要があります。このコンストラクターが public
であるという事実により、次の例に示すように、SpEL を使用してボイラープレート構成を減らすことができます。<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()
java.lang.IllegalStateException
- 使用可能なポートが見つからなかった場合 public static int findAvailableTcpPort(int minPort)
minPort
、65535 ] からランダムに選択された使用可能な TCP ポートを見つけます。minPort
- 最小ポート番号 java.lang.IllegalStateException
- 使用可能なポートが見つからなかった場合 public static int findAvailableTcpPort(int minPort, int maxPort)
minPort
、maxPort
] からランダムに選択された使用可能な TCP ポートを見つけます。minPort
- 最小ポート番号 maxPort
- 最大ポート番号 java.lang.IllegalStateException
- 使用可能なポートが見つからなかった場合 public static java.util.SortedSet<java.lang.Integer> findAvailableTcpPorts(int numRequested)
numRequested
- 検索できるポートの数 java.lang.IllegalStateException
- リクエストされた数の使用可能なポートが見つからなかった場合 public static java.util.SortedSet<java.lang.Integer> findAvailableTcpPorts(int numRequested, int minPort, int maxPort)
minPort
、maxPort
] からランダムに選択された、使用可能な TCP ポートのリクエストされた数を見つけます。numRequested
- 検索できるポートの数 minPort
- 最小ポート番号 maxPort
- 最大ポート番号 java.lang.IllegalStateException
- リクエストされた数の使用可能なポートが見つからなかった場合 public static int findAvailableUdpPort()
java.lang.IllegalStateException
- 使用可能なポートが見つからなかった場合 public static int findAvailableUdpPort(int minPort)
minPort
、65535 ] からランダムに選択された使用可能な UDP ポートを見つけます。minPort
- 最小ポート番号 java.lang.IllegalStateException
- 使用可能なポートが見つからなかった場合 public static int findAvailableUdpPort(int minPort, int maxPort)
minPort
、maxPort
] からランダムに選択された使用可能な UDP ポートを見つけます。minPort
- 最小ポート番号 maxPort
- 最大ポート番号 java.lang.IllegalStateException
- 使用可能なポートが見つからなかった場合 public static java.util.SortedSet<java.lang.Integer> findAvailableUdpPorts(int numRequested)
numRequested
- 検索できるポートの数 java.lang.IllegalStateException
- リクエストされた数の使用可能なポートが見つからなかった場合 public static java.util.SortedSet<java.lang.Integer> findAvailableUdpPorts(int numRequested, int minPort, int maxPort)
minPort
、maxPort
] からランダムに選択された、使用可能な UDP ポートのリクエストされた数を見つけます。numRequested
- 検索できるポートの数 minPort
- 最小ポート番号 maxPort
- 最大ポート番号 java.lang.IllegalStateException
- リクエストされた数の使用可能なポートが見つからなかった場合