close ServerSocket after testing availablity

Signed-off-by: stefan.pingel@consensys.net <stefan.pingel@consensys.net>
CloseServerSocket
stefan.pingel@consensys.net 10 months ago
parent 8f2ee84bd7
commit a21f8c799b
  1. 5
      besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
  2. 34
      util/src/main/java/org/hyperledger/besu/util/NetworkUtility.java
  3. 12
      util/src/test/java/org/hyperledger/besu/util/NetworkUtilityTest.java

@ -2555,11 +2555,12 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
.forEach(
port -> {
if (port.equals(p2PDiscoveryOptionGroup.p2pPort)
&& !NetworkUtility.isPortAvailable(port)) {
&& (NetworkUtility.isPortUnavailableForTcp(port)
|| NetworkUtility.isPortUnavailableForUdp(port))) {
unavailablePorts.add(port);
}
if (!port.equals(p2PDiscoveryOptionGroup.p2pPort)
&& !NetworkUtility.isPortAvailableForTcp(port)) {
&& NetworkUtility.isPortUnavailableForTcp(port)) {
unavailablePorts.add(port);
}
});

@ -160,41 +160,39 @@ public class NetworkUtility {
}
/**
* Is port available for tcp.
* Is this port unavailable for tcp.
*
* @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()) {
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(port));
return true;
serverSocket.close();
return false;
} catch (IOException 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 this 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)) {
datagramSocket.setReuseAddress(true);
datagramSocket.bind(new InetSocketAddress(port));
return true;
datagramSocket.close();
return false;
} catch (IOException ex) {
LOG.trace(String.format("failed to open port %d for UDP", port), ex);
}
return false;
}
/**
* Is port available.
*
* @param port the port
* @return the boolean
*/
public static boolean isPortAvailable(final int port) {
return isPortAvailableForTcp(port) && isPortAvailableForUdp(port);
return true;
}
/**

@ -17,6 +17,7 @@ package org.hyperledger.besu.util;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
@ -35,12 +36,19 @@ public class NetworkUtilityTest {
}
@Test
public void assertPortIsNotAvailable() throws IOException {
public void assertTcpPortIsNotAvailable() throws IOException {
final ServerSocket serverSocket = new ServerSocket(8541);
assertThat(!NetworkUtility.isPortAvailable(8541)).isEqualTo(true);
assertThat(NetworkUtility.isPortUnavailableForTcp(8541)).isEqualTo(true);
serverSocket.close();
}
@Test
public void assertUdpPortIsNotAvailable() throws IOException {
final DatagramSocket datagramSocket = new DatagramSocket(8541);
assertThat(NetworkUtility.isPortUnavailableForUdp(8541)).isEqualTo(true);
datagramSocket.close();
}
@Test
public void assertLocalhostIdentification() {
assertThat(NetworkUtility.isLocalhostAddress("127.0.0.1")).isTrue();

Loading…
Cancel
Save