checkPort method
检测单个端口是否可连接 / Checks whether a single port accepts connections.
Implementation
Future<PortCheckResult> checkPort({
required String host,
required int port,
Duration? timeout,
}) async {
final effectiveTimeout = timeout ?? defaultTimeout;
final stopwatch = Stopwatch()..start();
Socket? socket;
try {
socket = await Socket.connect(host, port, timeout: effectiveTimeout);
stopwatch.stop();
return PortCheckResult(
host: host,
port: port,
isOpen: true,
responseTime: stopwatch.elapsed,
timestamp: DateTime.now(),
);
} catch (error) {
stopwatch.stop();
return PortCheckResult(
host: host,
port: port,
isOpen: false,
responseTime: stopwatch.elapsed,
errorMessage: _describe(error),
timestamp: DateTime.now(),
);
} finally {
socket?.destroy();
}
}