You are here

protected function ServerCommand::findAvailablePort in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Command/ServerCommand.php \Drupal\Core\Command\ServerCommand::findAvailablePort()
  2. 10 core/lib/Drupal/Core/Command/ServerCommand.php \Drupal\Core\Command\ServerCommand::findAvailablePort()

Finds an available port.

Parameters

string $host: The host to find a port on.

Return value

int|false The available port or FALSE, if no available port found,

1 call to ServerCommand::findAvailablePort()
ServerCommand::execute in core/lib/Drupal/Core/Command/ServerCommand.php

File

core/lib/Drupal/Core/Command/ServerCommand.php, line 116

Class

ServerCommand
Runs the PHP webserver for a Drupal site for local testing/development.

Namespace

Drupal\Core\Command

Code

protected function findAvailablePort($host) {
  $port = 8888;
  while ($port >= 8888 && $port <= 9999) {
    $connection = @fsockopen($host, $port);
    if (is_resource($connection)) {

      // Port is being used.
      fclose($connection);
    }
    else {

      // Port is available.
      return $port;
    }
    $port++;
  }
  return FALSE;
}