mirror of
https://github.com/vacp2p/linea-besu.git
synced 2026-01-08 23:17:54 -05:00
Close Sockets after testing for availablity (#6516)
Signed-off-by: stefan.pingel@consensys.net <stefan.pingel@consensys.net>
This commit is contained in:
@@ -160,41 +160,39 @@ public class NetworkUtility {
|
||||
}
|
||||
|
||||
/**
|
||||
* Is port available for tcp.
|
||||
* Is 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;
|
||||
}
|
||||
|
||||
private static boolean isPortAvailableForUdp(final int port) {
|
||||
try (final DatagramSocket datagramSocket = new DatagramSocket(null)) {
|
||||
datagramSocket.setReuseAddress(true);
|
||||
datagramSocket.bind(new InetSocketAddress(port));
|
||||
return true;
|
||||
} catch (IOException ex) {
|
||||
LOG.trace(String.format("failed to open port %d for UDP", port), ex);
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is port available.
|
||||
* Is port unavailable for udp.
|
||||
*
|
||||
* @param port the port
|
||||
* @return the boolean
|
||||
* @return true if the port is unavailable for UDP
|
||||
*/
|
||||
public static boolean isPortAvailable(final int port) {
|
||||
return isPortAvailableForTcp(port) && isPortAvailableForUdp(port);
|
||||
public static boolean isPortUnavailableForUdp(final int port) {
|
||||
try (final DatagramSocket datagramSocket = new DatagramSocket(null)) {
|
||||
datagramSocket.setReuseAddress(true);
|
||||
datagramSocket.bind(new InetSocketAddress(port));
|
||||
datagramSocket.close();
|
||||
return false;
|
||||
} catch (IOException ex) {
|
||||
LOG.trace(String.format("failed to open port %d for UDP", port), ex);
|
||||
}
|
||||
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 assertPortIsNotAvailableForTcp() 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 assertPortIsNotAvailableForUdp() 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();
|
||||
|
||||
Reference in New Issue
Block a user