waitForPortAvailable

fun waitForPortAvailable(port: Int, maxAttempts: Int = 50, delayMs: Long = 200): Boolean

Waits until the specified TCP port can be bound, polling with a delay between attempts.

Availability is checked by binding the wildcard address. On macOS and other BSDs that bind can succeed while another process still listens on 127.0.0.1 at the same port, so a true there does not guarantee a loopback bind will succeed.

Return

true once the port is available, or false if it was still in use after maxAttempts attempts

Parameters

port

the TCP port to wait for

maxAttempts

the maximum number of attempts (default 50)

delayMs

the delay in milliseconds between attempts (default 200)

Throws

if port is outside 0..65535