From 61736389427004fce9eaa7e896a151f0ee5156e9 Mon Sep 17 00:00:00 2001 From: Juan Cristobal Date: Fri, 1 Mar 2024 15:14:45 +0100 Subject: [PATCH] update file --- .../Console/Commands/Server/Serve.php | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/libraries/Console/Commands/Server/Serve.php b/src/libraries/Console/Commands/Server/Serve.php index fc2f13b..d09653b 100644 --- a/src/libraries/Console/Commands/Server/Serve.php +++ b/src/libraries/Console/Commands/Server/Serve.php @@ -101,34 +101,26 @@ public function run(array $params) /** * Find an available port */ - protected function findAvailablePort(string $host, int $port): ?int + protected function findAvailablePort(string $host, int $startPort): ?int { - $maxTries = $this->maxTries; - $waitTime = $this->waitTimeInSeconds; // Tiempo de espera entre intentos en segundos - - for ($i = 0; $i < $maxTries; $i++) { + $maxTries = $this->maxTries; + for ($port = $startPort; $port < $startPort + $maxTries; $port++) { if ($this->checkPort($host, $port)) { return $port; } - - sleep($waitTime); // Espera antes de intentar nuevamente } - + return null; } - - /** - * Check if a port is available - */ + protected function checkPort(string $host, int $port): bool { - $socket = @fsockopen($host, $port); - if ($socket !== false) { - fclose($socket); - return false; - } - return true; + $url = "http://$host:$port"; + $headers = @get_headers($url); + + return !empty($headers); } + /** * Shutdown the server