isFreeListenPort static method

Future<bool> isFreeListenPort(
  1. int port, {
  2. Duration? testTimeout,
})

Returns true if port is free to listen.

Implementation

static Future<bool> isFreeListenPort(int port,
    {Duration? testTimeout}) async {
  testTimeout ??= Duration(seconds: 1);

  try {
    var socket =
        await Socket.connect('localhost', port, timeout: testTimeout);
    try {
      socket.close();
    } catch (_) {}
    return false;
  } catch (_) {
    return true;
  }
}