isPortInUse static method

Future<bool> isPortInUse(
  1. int port, {
  2. String address = '127.0.0.1',
})

Implementation

static Future<bool> isPortInUse(int port, {String address = '127.0.0.1'}) async {
  ServerSocket? serverSocket;
  try {
    // 尝试绑定到指定地址和端口
    serverSocket = await ServerSocket.bind(address, port);
    // 如果绑定成功,说明端口未被占用,立即关闭并返回 false
    await serverSocket.close();
    return false;
  } on SocketException catch (e) {
    // 捕获到 SocketException,通常意味着端口已被占用或无权访问
    debugPrint('SocketException on port $port: ${e.message}');
    return true;
  } catch (e) {
    // 捕获其他可能的异常
    debugPrint('An unexpected error occurred on port $port: $e');
    return true; // 将其他异常也视为端口不可用
  }
}