com.xiongyingqi.util.SocketUtils Maven / Gradle / Ivy
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xiongyingqi.util;
import javax.net.ServerSocketFactory;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;
public abstract class SocketUtils {
public static final int PORT_RANGE_MIN = 1024;
public static final int PORT_RANGE_MAX = 65535;
private static final Random random = new Random(System.currentTimeMillis());
public SocketUtils() {
/* no-op */
}
public static int findAvailableTcpPort() {
return findAvailableTcpPort(PORT_RANGE_MIN);
}
public static int findAvailableTcpPort(int minPort) {
return findAvailableTcpPort(minPort, PORT_RANGE_MAX);
}
public static int findAvailableTcpPort(int minPort, int maxPort) {
return SocketType.TCP.findAvailablePort(minPort, maxPort);
}
public static SortedSet findAvailableTcpPorts(int numRequested) {
return findAvailableTcpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
}
public static SortedSet findAvailableTcpPorts(int numRequested, int minPort, int maxPort) {
return SocketType.TCP.findAvailablePorts(numRequested, minPort, maxPort);
}
public static int findAvailableUdpPort() {
return findAvailableUdpPort(PORT_RANGE_MIN);
}
public static int findAvailableUdpPort(int minPort) {
return findAvailableUdpPort(minPort, PORT_RANGE_MAX);
}
public static int findAvailableUdpPort(int minPort, int maxPort) {
return SocketType.UDP.findAvailablePort(minPort, maxPort);
}
public static SortedSet findAvailableUdpPorts(int numRequested) {
return findAvailableUdpPorts(numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
}
public static SortedSet findAvailableUdpPorts(int numRequested, int minPort, int maxPort) {
return SocketType.UDP.findAvailablePorts(numRequested, minPort, maxPort);
}
private static enum SocketType {
TCP {
@Override
protected boolean isPortAvailable(int port) {
try {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port);
serverSocket.close();
return true;
} catch (Exception ex) {
return false;
}
}
},
UDP {
@Override
protected boolean isPortAvailable(int port) {
try {
DatagramSocket socket = new DatagramSocket(port);
socket.close();
return true;
} catch (Exception ex) {
return false;
}
}
};
protected abstract boolean isPortAvailable(int port);
private int findRandomPort(int minPort, int maxPort) {
int portRange = maxPort - minPort;
return minPort + random.nextInt(portRange);
}
int findAvailablePort(int minPort, int maxPort) {
Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'");
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
int portRange = maxPort - minPort;
int candidatePort;
int searchCounter = 0;
do {
if (++searchCounter > portRange) {
throw new IllegalStateException(String.format(
"Could not find an available %s port in the range [%d, %d] after %d attempts", name(), minPort,
maxPort, searchCounter));
}
candidatePort = findRandomPort(minPort, maxPort);
} while (!isPortAvailable(candidatePort));
return candidatePort;
}
SortedSet findAvailablePorts(int numRequested, int minPort, int maxPort) {
Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'");
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0");
Assert.isTrue((maxPort - minPort) >= numRequested,
"'numRequested' must not be greater than 'maxPort' - 'minPort'");
final SortedSet availablePorts = new TreeSet();
int attemptCount = 0;
while ((++attemptCount <= numRequested + 100) && (availablePorts.size() < numRequested)) {
availablePorts.add(findAvailablePort(minPort, maxPort));
}
if (availablePorts.size() != numRequested) {
throw new IllegalStateException(String.format(
"Could not find %d available %s ports in the range [%d, %d]", numRequested, name(), minPort,
maxPort));
}
return availablePorts;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy