Java HTTP Port Find isLocalPortAvailable(final int port)

Here you can find the source of isLocalPortAvailable(final int port)

Description

Checks to see if a specific local port is available.

License

Apache License

Parameter

Parameter Description
port the port to check for availability

Return

true if port is available

Declaration

public static boolean isLocalPortAvailable(final int port) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.net.DatagramSocket;

import java.net.ServerSocket;

public class Main {
    /**//from   w  w  w .j  ava2 s .  c om
     * Checks to see if a specific <b>local</b> port is available.
     *
     * @param port the port to check for availability
     * @return {@code true} if port is available
     */
    public static boolean isLocalPortAvailable(final int port) {
        try (final DatagramSocket ds = new DatagramSocket(port); final ServerSocket ss = new ServerSocket(port)) {
            ss.setReuseAddress(true);
            ds.setReuseAddress(true);
            return true;
        } catch (final IOException ignored) {
            return false;
        }
    }
}

Related

  1. isFreeTCPPort(final int port)
  2. isHostAvailable(final String host, final int port)
  3. isHostReachable(String host, int port, int connTimeout)
  4. isListening(String host, int port)
  5. isLivereloadAvailable(int port)
  6. isLocalPortFree(final int port)
  7. isLocalPortOccupied(int portNum)
  8. isLocalPortUsed(int port)
  9. isLoclePortUsing(int port)