In PHP, stream_socket_pair() creates a pair of connected, indistinguishable socket streams.
Anything written on one end can be read on the other. It is ideal for offline demos, IPC between related processes, and teaching send/recv without a listening server. Use STREAM_PF_INET for broad Windows and Unix support in beginner examples.
Syntax
Call stream_socket_pair() with the values you need. Start with this line.
On success you get an array of two streams. STREAM_PF_INET is the portable choice for tutorials. Close both ends with fclose(). Prefer this over client/server when you only need a connected channel offline.
Library declarations
PHP
stream_socket_pair(int $domain, int $type, int $protocol): array|false
Parameters
Parameter
Description
domain
Required. Protocol family such as STREAM_PF_INET or STREAM_PF_UNIX.
type
Required. Socket type such as STREAM_SOCK_STREAM.
protocol
Required. Protocol such as STREAM_IPPROTO_IP.
Return Value
Return Value
Description
array
Two connected stream resources: index 0 and index 1.
false
The pair could not be created.
PHP Examples
Example 1 — Send Bytes Across a Pair
Create a pair, write on one end, and read on the other.