isFreeListenPort function
Returns true
if port
is free to listen.
Implementation
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;
}
}