stream_socket_server()

stream_socket_server() — Create an Internet or Unix domain server socket.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

stream_socket_server(string $address, int &$error_code = null, string &$error_message = null, int $flags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, ?resource $context = null): resource|false

Parameters

ParameterTypeWhat it takes
$addressstringThe type of socket created is determined by the transport specified using standard URL formatting: transport://target.
&$error_codeintIf the optional error_code and error_message arguments are present they will be set to indicate the actual system level error that occurred in the system-level socket(), bind(), and listen() calls. If the value returned in error_code is 0 and the function returned false, it is an indication that the error occurred before the bind() call. This is most likely due to a problem initializing the socket. Note that the ...
&$error_messagestringSee error_code description.
$flagsintA bitmask field which may be set to any combination of socket creation flags.
$context?resource

Return value

Returns the created stream, or false on error.

Example

A runnable snippet:

$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr); if (!$socket) { echo "$errstr ($errno)<br />\n"; } else { while ($conn = stream_socket_accept($socket)) { fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n"); fclose($conn); } fclose($socket); }

Changelog

VersionChange
8.0.0context is nullable now.

See also

stream_socket_client(), stream_set_blocking(), stream_set_timeout(), fgets(), fwrite(), fclose(), feof()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page stream_socket_server() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.