1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-27 00:38:11 -07:00

Better port loop.

This commit is contained in:
Cedric Beust 2016-04-27 18:39:24 -08:00
parent f856082478
commit 85db1c767e

View file

@ -5,14 +5,8 @@ import java.net.Socket
class ProcessUtil {
companion object {
fun findAvailablePort(port: Int = 1234): Int {
if (isPortAvailable(port)) return port
for (i in 1235..65000) {
if (isPortAvailable(i)) return i
}
throw IllegalArgumentException("Couldn't find any port available, something is very wrong")
}
fun findAvailablePort(port: Int = 1234) = (port .. 65000).firstOrNull { isPortAvailable(it) }
?: throw IllegalArgumentException("Couldn't find any port available, something is very wrong")
private fun isPortAvailable(port: Int): Boolean {
var s: Socket? = null