function ServerCommand::findAvailablePort

Same name in other branches
  1. 9 core/lib/Drupal/Core/Command/ServerCommand.php \Drupal\Core\Command\ServerCommand::findAvailablePort()
  2. 8.9.x core/lib/Drupal/Core/Command/ServerCommand.php \Drupal\Core\Command\ServerCommand::findAvailablePort()
  3. 11.x 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.