Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package datadog.trace.agent.test.utils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
Expand All @@ -9,6 +11,7 @@
public class PortUtils {

public static int UNUSABLE_PORT = 61;
private static final int PORT_CONNECT_TIMEOUT_MS = 1000;

private static final int FREE_PORT_RANGE_START = 20000;
private static final int FREE_PORT_RANGE_END = 40000;
Expand Down Expand Up @@ -96,7 +99,12 @@ private static boolean isPortOpen(final int port) {
}

private static boolean isPortOpen(String host, int port) {
try (final Socket socket = new Socket(host, port)) {
try (final Socket socket = new Socket()) {
InetSocketAddress address =
host == null
? new InetSocketAddress(InetAddress.getLoopbackAddress(), port)
: new InetSocketAddress(host, port);
socket.connect(address, PORT_CONNECT_TIMEOUT_MS);
return true;
} catch (final IOException e) {
return false;
Expand Down
Loading