Class Socket

CakePHP network socket connection class.

Core base class for network communication.

Namespace: Cake\Network

Properties summary

  • $_config protected
    array

    Runtime config

  • bool

    Whether the config property has already been configured with defaults

  • $_connectionErrors protected
    array

    Used to capture connection warnings which can happen when there are SSL errors for example.

  • $_defaultConfig protected
    array

    Default configuration settings for the socket connection

  • $_encryptMethods protected
    array

    Contains all the encryption methods available

  • $connected public
    bool

    This boolean contains the current state of the Socket class

  • $connection public
    resource|null

    Reference to socket connection resource

  • $description public
    string

    Object description

  • $encrypted public
    bool

    True if the socket stream is encrypted after a Cake\Network\Socket::enableCrypto() call

  • $lastError public
    array

    This variable contains an array with the last error number (num) and string (str)

Method Summary

  • __construct() public

    Constructor.

  • __destruct() public

    Destructor, used to disconnect from current connection.

  • _configDelete() protected

    Deletes a single config key.

  • _configRead() protected

    Reads a config key.

  • _configWrite() protected

    Writes a config key.

  • _connectionErrorHandler() protected

    socket_stream_client() does not populate errNum, or $errStr when there are connection errors, as in the case of SSL verification failure.

  • _getStreamSocketClient() protected

    Create a stream socket client. Mock utility.

  • _setSslContext() protected

    Configure the SSL context options.

  • address() public

    Get the IP address of the current connection.

  • addresses() public

    Get all IP addresses associated with the current connection.

  • config() public

    Gets/Sets the config.

  • configShallow() public

    Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.

  • connect() public

    Connect the socket to the given host and port.

  • context() public

    Get the connection context.

  • disconnect() public

    Disconnect the socket from the current connection.

  • enableCrypto() public

    Encrypts current stream socket, using one of the defined encryption methods

  • getConfig() public

    Returns the config.

  • getConfigOrFail() public

    Returns the config for this specific key.

  • host() public

    Get the host name of the current connection.

  • lastError() public

    Get the last error as a string.

  • read() public

    Read data from the socket. Returns false if no data is available or no connection could be established.

  • reset() public

    Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)

  • setConfig() public

    Sets the config.

  • setLastError() public

    Set the last error.

  • write() public

    Write data to the socket.

Method Detail

__construct() public

__construct(array $config)

Constructor.

Parameters

array $config optional

Socket configuration, which will be merged with the base configuration

See Also

\Cake\Network\Socket::$_baseConfig

__destruct() public

__destruct()

Destructor, used to disconnect from current connection.

_configDelete() protected

_configDelete(mixed $key)

Deletes a single config key.

Parameters

string $key

Key to delete.

Throws

Cake\Core\Exception\Exception
if attempting to clobber existing config

_configRead() protected

_configRead(mixed $key)

Reads a config key.

Parameters

string|null $key

Key to read.

Returns

mixed

_configWrite() protected

_configWrite(mixed $key, mixed $value, mixed $merge)

Writes a config key.

Parameters

string|array $key

Key to write to.

mixed $value

Value to write.

bool|string $merge optional

True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.

Throws

Cake\Core\Exception\Exception
if attempting to clobber existing config

_connectionErrorHandler() protected

_connectionErrorHandler(mixed $code, mixed $message)

socket_stream_client() does not populate errNum, or $errStr when there are connection errors, as in the case of SSL verification failure.

Instead we need to handle those errors manually.

Parameters

int $code

Code number.

string $message

Message.

_getStreamSocketClient() protected

_getStreamSocketClient(mixed $remoteSocketTarget, mixed $errNum, mixed $errStr, mixed $timeout, mixed $connectAs, mixed $context)

Create a stream socket client. Mock utility.

Parameters

string $remoteSocketTarget

remote socket

int $errNum

error number

string $errStr

error string

int $timeout

timeout

int $connectAs

flags

resource $context

context

Returns

bool|resource

_setSslContext() protected

_setSslContext(mixed $host)

Configure the SSL context options.

Parameters

string $host

The host name being connected to.

address() public

address()

Get the IP address of the current connection.

Returns

string

IP address

addresses() public

addresses()

Get all IP addresses associated with the current connection.

Returns

array

IP addresses

config() public

config(mixed $key, mixed $value, mixed $merge)

Gets/Sets the config.

Usage

Reading the whole config:

$this->config();

Reading a specific value:

$this->config('key');

Reading a nested value:

$this->config('some.nested.key');

Setting a specific value:

$this->config('key', $value);

Setting a nested value:

$this->config('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->config(['one' => 'value', 'another' => 'value']);

Parameters

string|array|null $key optional

The key to get/set, or a complete array of configs.

mixed|null $value optional

The value to set.

bool $merge optional

Whether to recursively merge or overwrite existing config, defaults to true.

Returns

mixed

Config value being read, or the object itself on write operations.

Throws

Cake\Core\Exception\Exception
When trying to set a key that is invalid.

configShallow() public

configShallow(mixed $key, mixed $value)

Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.

Setting a specific value:

$this->configShallow('key', $value);

Setting a nested value:

$this->configShallow('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->configShallow(['one' => 'value', 'another' => 'value']);

Parameters

string|array $key

The key to set, or a complete array of configs.

mixed|null $value optional

The value to set.

Returns

$this

connect() public

connect()

Connect the socket to the given host and port.

Returns

bool

Success

Throws

Cake\Network\Exception\SocketException

context() public

context()

Get the connection context.

Returns

array|null

Null when there is no connection, an array when there is.

disconnect() public

disconnect()

Disconnect the socket from the current connection.

Returns

bool

Success

enableCrypto() public

enableCrypto(mixed $type, mixed $clientOrServer, mixed $enable)

Encrypts current stream socket, using one of the defined encryption methods

Parameters

string $type

can be one of 'ssl2', 'ssl3', 'ssl23' or 'tls'

string $clientOrServer optional

can be one of 'client', 'server'. Default is 'client'

bool $enable optional

enable or disable encryption. Default is true (enable)

Returns

bool

True on success

Throws

InvalidArgumentException
When an invalid encryption scheme is chosen.
Cake\Network\Exception\SocketException
When attempting to enable SSL/TLS fails

See Also

\Cake\Network\stream_socket_enable_crypto

getConfig() public

getConfig(mixed $key, mixed $default)

Returns the config.

Usage

Reading the whole config:

$this->getConfig();

Reading a specific value:

$this->getConfig('key');

Reading a nested value:

$this->getConfig('some.nested.key');

Reading with default value:

$this->getConfig('some-key', 'default-value');

Parameters

string|null $key optional

The key to get or null for the whole config.

mixed|null $default optional

The return value when the key does not exist.

Returns

mixed|null

Configuration data at the named key or null if the key does not exist.

getConfigOrFail() public

getConfigOrFail(mixed $key)

Returns the config for this specific key.

The config value for this key must exist, it can never be null.

Parameters

string|null $key

The key to get.

Returns

mixed

Configuration data at the named key

Throws

InvalidArgumentException

host() public

host()

Get the host name of the current connection.

Returns

string

Host name

lastError() public

lastError()

Get the last error as a string.

Returns

string|null

Last error

read() public

read(mixed $length)

Read data from the socket. Returns false if no data is available or no connection could be established.

The bool false return value is deprecated and will be null in the next major. Please code respectively to be future proof.

Parameters

int $length optional

Optional buffer length to read; defaults to 1024

Returns

mixed

Socket data

reset() public

reset(mixed $state)

Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)

Parameters

array|null $state optional

Array with key and values to reset

Returns

bool

True on success

setConfig() public

setConfig(mixed $key, mixed $value, mixed $merge)

Sets the config.

Usage

Setting a specific value:

$this->setConfig('key', $value);

Setting a nested value:

$this->setConfig('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->setConfig(['one' => 'value', 'another' => 'value']);

Parameters

string|array $key

The key to set, or a complete array of configs.

mixed|null $value optional

The value to set.

bool $merge optional

Whether to recursively merge or overwrite existing config, defaults to true.

Returns

$this

Throws

Cake\Core\Exception\Exception
When trying to set a key that is invalid.

setLastError() public

setLastError(mixed $errNum, mixed $errStr)

Set the last error.

Parameters

int $errNum

Error code

string $errStr

Error string

write() public

write(mixed $data)

Write data to the socket.

The bool false return value is deprecated and will be int 0 in the next major. Please code respectively to be future proof.

Parameters

string $data

The data to write to the socket.

Returns

int|false

Bytes written.

Property Detail

$_config protected

Runtime config

Type

array

$_configInitialized protected

Whether the config property has already been configured with defaults

Type

bool

$_connectionErrors protected

Used to capture connection warnings which can happen when there are SSL errors for example.

Type

array

$_defaultConfig protected

Default configuration settings for the socket connection

Type

array

$_encryptMethods protected

Contains all the encryption methods available

SSLv2 and SSLv3 are deprecated, and should not be used as they have several published vulnerablilities.

Type

array

$connected public

This boolean contains the current state of the Socket class

Type

bool

$connection public

Reference to socket connection resource

Type

resource|null

$description public

Object description

Type

string

$encrypted public

True if the socket stream is encrypted after a Cake\Network\Socket::enableCrypto() call

Type

bool

$lastError public

This variable contains an array with the last error number (num) and string (str)

Type

array

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.9/class-Cake.Network.Socket.html