Java Utililty Methods HTTP Port Find

List of utility methods to do HTTP Port Find

Description

The list of methods to do HTTP Port Find are organized into topic(s).

Method

booleanisPortAvailable(String host, int port)
Is the specified port available?
try {
    (new Socket(host, port)).close();
    return true;
} catch (IOException e) {
    return false;
booleanisPortAvailable(String host, int port)
is Port Available
if (host != null) {
    try {
        new ServerSocket(port, 50, InetAddress.getByName(host)).close();
        return true;
    } catch (Exception e) {
} else {
    try {
...
booleanisPortAvailable(String hostname, int port)
Checks whether the supplied port is available on the specified address.
ServerSocket socket;
try {
    socket = new ServerSocket();
} catch (IOException e) {
    throw new IllegalStateException("Unable to create ServerSocket.", e);
try {
    InetSocketAddress sa = new InetSocketAddress(hostname, port);
...
booleanisPortAvailable(String hostname, int port)
Checks to see if a port/hostname combo is available through opening a socked and closing it again
if (port == -1)
    return false;
try {
    ServerSocket socket = new ServerSocket();
    socket.bind(hostname == null || hostname.length() == 0 ? new InetSocketAddress(port)
            : new InetSocketAddress(hostname, port));
    socket.close();
    return true;
...
booleanisPortAvailable(String hostName, int port)
is Port Available
InetSocketAddress socketAddress = null;
if (hostName == null || "".equals(hostName) || "*".equals(hostName)) {
    socketAddress = new InetSocketAddress(port);
} else {
    socketAddress = new InetSocketAddress(hostName, port);
ServerSocket serverSocket = null;
try {
...
booleanisPortAvailable(String portNumber)
Function to check if a port is available
final int port = Integer.parseInt(portNumber);
ServerSocket ss = null;
try {
    ss = new ServerSocket(port);
    ss.setReuseAddress(true);
    return true;
} catch (IOException e) {
} finally {
...
booleanisPortBound(int port)
is Port Bound
try (ServerSocket socket = new ServerSocket(port)) {
    socket.getClass(); 
    return false;
} catch (IOException ex) {
    return true;
booleanisPortFree(int port)
is Port Free
try {
    Socket socket = new Socket("localhost", port);
    socket.close();
    return false;
} catch (ConnectException e) {
    return true;
} catch (SocketException e) {
    if (e.getMessage().equals("Connection reset by peer")) {
...
booleanisPortFree(int port)
Check and log is a given port is available
boolean portIsFree = true;
ServerSocket server = null;
try {
    server = new ServerSocket(port);
} catch (IOException e) {
    portIsFree = false;
} finally {
    if (server != null) {
...
booleanisPortFree(int portNumber)
Checks to see if the port is available.
try {
    Socket echoSocket = new Socket("localhost", portNumber);
    echoSocket.close();
    return false;
} catch (UnknownHostException e) {
    return true;
} catch (IOException e) {
    return true;
...