PHP stream_socket_accept() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_socket_accept() accepts an incoming connection on a listening server socket created with stream_socket_server().

It returns a new stream for that client, or false on failure or timeout. You can pass a timeout in seconds and an optional variable that receives the peer address string. Call it in a loop when your server handles more than one client.

Syntax

Call stream_socket_accept() with the values you need. Start with this line.

PHP
$client = stream_socket_accept($server, 5, $peer);
CallWhat it does
stream_socket_accept($server)Waits for one client and returns a connected stream.
stream_socket_accept($server, 2.0, $peer)Waits up to 2 seconds and stores the peer address in $peer.
stream_socket_accept($server, -1)Waits indefinitely until a client connects.

The first argument must be a listening socket from stream_socket_server(). A real accept needs a client that connects. Offline tutorials often show the call shape with fixed sample output and a comment that a listening server is required.

Library declarations
PHP
stream_socket_accept(resource $socket, ?float $timeout = null, ?string &$peer_name = null): resource|false

Parameters

ParameterDescription
socketRequired. A server stream created with stream_socket_server() that is already listening.
timeoutOptional. Seconds to wait. null uses default_socket_timeout. -1 waits forever.
peer_nameOptional. Passed by reference; filled with the remote address string when provided.

Return Value

Return ValueDescription
resourceA new stream connected to the accepted client.
falseNo connection was accepted (timeout, error, or no listener).

PHP Examples

Example 1 — Call Shape for Accept

Show the typical accept pattern with comments. A real run needs a listening server and a connecting client.

PHP
<?php
// Needs a listening server from stream_socket_server().
// Illustrative output for tutorials (not a live accept):
$peer = '127.0.0.1:54321';
echo 'accepted peer: ', $peer, PHP_EOL;
echo 'next: fread/fwrite on the client stream', PHP_EOL;

How It Works

  1. stream_socket_server() creates the listening socket first.
  2. stream_socket_accept() waits for one incoming client.
  3. The peer address looks like host:port.
  4. Use the returned client stream for fread() and fwrite().

Example 2 — Offline Pair Instead of Accept

Use stream_socket_pair() when you want two connected streams without a real TCP listener.

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
if ($pair === false) {
    exit("pair failed\n");
}
[$a, $b] = $pair;

fwrite($a, "hello");
echo fread($b, 5), PHP_EOL;

fclose($a);
fclose($b);

How It Works

  1. stream_socket_pair() creates two already-connected ends offline.
  2. Writing on one end is readable on the other.
  3. This replaces accept for local teaching demos.
  4. Close both ends when finished.

Example 3 — Timeout Comment for Accept

Document a two-second accept timeout with fixed sample text for beginners.

PHP
<?php
// Example: $client = stream_socket_accept($server, 2.0, $peer);
// If no client connects in time, the call returns false.
echo 'accept timeout sample: 2.0 seconds', PHP_EOL;
echo 'on timeout: false', PHP_EOL;

How It Works

  1. The second argument is the wait limit in seconds.
  2. false means nobody connected before the timeout.
  3. Check the result before using the client stream.
  4. A live server must already be listening.

Frequently Asked Questions

It accepts one incoming connection on a listening server socket and returns a new client stream.
Create and listen with stream_socket_server() first. Accept only works on that listening stream.
An optional by-reference string that receives the remote address, such as 127.0.0.1:54321.
A real accept needs a listener and a client. For offline practice, use stream_socket_pair() instead.
The call waits indefinitely until a client connects or an error occurs.

Next: stream_socket_client()

stream_socket_client() opens a client connection to a remote or local socket address.

stream_socket_client() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful