findAvailablePort function

Future<int> findAvailablePort(
  1. String host,
  2. int startPort
)

Returns the first available port on host starting from startPort. Continues incrementing the port number until a free socket is found.

Implementation

Future<int> findAvailablePort(String host, int startPort) async {
  int candidate = startPort;
  while (true) {
    if (await isPortAvailable(host, candidate)) {
      return candidate;
    }
    candidate++;
  }
}