PHP Function Reference

PHP stream_socket_accept() Function



The PHP stream_socket_accept() function accepts a connection on a socket previously created by stream_socket_server().

Syntax

stream_socket_accept(server_socket, timeout, peername)

Parameters

server_socket Required. Specify the server socket to accept a connection from.
timeout Optional. Specify the socket accept timeout, in seconds. When null, the default_socket_timeout of php.ini setting is used.
peername Optional. Will be set to the name (address) of the client which connected, if included and available from the selected transport.

Return Value

Returns a stream to the accepted socket connection or false on failure.

Example: stream_socket_accept() example

The example below shows the usage of stream_socket_accept() function. It shows how to act as a time server which can respond to the time queries.

<?php
$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);
}
?>

❮ PHP Streams Reference