|
|
@ -160,41 +160,39 @@ public class NetworkUtility { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Is port available for tcp. |
|
|
|
* Is port unavailable for tcp. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param port the port |
|
|
|
* @param port the port |
|
|
|
* @return the boolean |
|
|
|
* @return true if the port is unavailable for TCP |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isPortAvailableForTcp(final int port) { |
|
|
|
public static boolean isPortUnavailableForTcp(final int port) { |
|
|
|
try (final ServerSocket serverSocket = new ServerSocket()) { |
|
|
|
try (final ServerSocket serverSocket = new ServerSocket()) { |
|
|
|
serverSocket.setReuseAddress(true); |
|
|
|
serverSocket.setReuseAddress(true); |
|
|
|
serverSocket.bind(new InetSocketAddress(port)); |
|
|
|
serverSocket.bind(new InetSocketAddress(port)); |
|
|
|
return true; |
|
|
|
serverSocket.close(); |
|
|
|
|
|
|
|
return false; |
|
|
|
} catch (IOException ex) { |
|
|
|
} catch (IOException ex) { |
|
|
|
LOG.trace(String.format("Failed to open port %d for TCP", port), ex); |
|
|
|
LOG.trace(String.format("Failed to open port %d for TCP", port), ex); |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static boolean isPortAvailableForUdp(final int port) { |
|
|
|
/** |
|
|
|
|
|
|
|
* Is port unavailable for udp. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param port the port |
|
|
|
|
|
|
|
* @return true if the port is unavailable for UDP |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static boolean isPortUnavailableForUdp(final int port) { |
|
|
|
try (final DatagramSocket datagramSocket = new DatagramSocket(null)) { |
|
|
|
try (final DatagramSocket datagramSocket = new DatagramSocket(null)) { |
|
|
|
datagramSocket.setReuseAddress(true); |
|
|
|
datagramSocket.setReuseAddress(true); |
|
|
|
datagramSocket.bind(new InetSocketAddress(port)); |
|
|
|
datagramSocket.bind(new InetSocketAddress(port)); |
|
|
|
return true; |
|
|
|
datagramSocket.close(); |
|
|
|
|
|
|
|
return false; |
|
|
|
} catch (IOException ex) { |
|
|
|
} catch (IOException ex) { |
|
|
|
LOG.trace(String.format("failed to open port %d for UDP", port), ex); |
|
|
|
LOG.trace(String.format("failed to open port %d for UDP", port), ex); |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Is port available. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param port the port |
|
|
|
|
|
|
|
* @return the boolean |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static boolean isPortAvailable(final int port) { |
|
|
|
|
|
|
|
return isPortAvailableForTcp(port) && isPortAvailableForUdp(port); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|