com.xwzhou.commons.net.helper.PortHelper Maven / Gradle / Ivy
The newest version!
package com.xwzhou.commons.net.helper;
import java.io.IOException;
import java.net.ServerSocket;
import com.xwzhou.commons.lang.helper.ExceptionHelper;
public class PortHelper {
public static int getAvailablePort() {
int port = -1;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(0);
port = serverSocket.getLocalPort();
} catch (IOException e) {
ExceptionHelper.throwException(e);
} finally {
try {
if (null != serverSocket) {
serverSocket.close();
}
} catch (IOException e1) {
ExceptionHelper.throwException(e1);
}
}
return port;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy