PHP stream_socket_pair() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

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.

PHP
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
CallWhat it does
stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)Creates two connected TCP-style streams.
stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)Creates a Unix-domain pair where supported.
[$a, $b] = stream_socket_pair(...)Unpacks the two ends into $a and $b.

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

ParameterDescription
domainRequired. Protocol family such as STREAM_PF_INET or STREAM_PF_UNIX.
typeRequired. Socket type such as STREAM_SOCK_STREAM.
protocolRequired. Protocol such as STREAM_IPPROTO_IP.

Return Value

Return ValueDescription
arrayTwo connected stream resources: index 0 and index 1.
falseThe 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.

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
[$left, $right] = $pair;

fwrite($left, 'ping');
echo fread($right, 4), PHP_EOL;

fclose($left);
fclose($right);

How It Works

  1. stream_socket_pair() returns two connected streams.
  2. fwrite() on $left sends bytes into the pair.
  3. fread() on $right receives those bytes.
  4. Close both ends when done.

Example 2 — Talk Both Directions

Exchange a short message in each direction on the same pair.

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
[$a, $b] = $pair;

fwrite($a, 'A');
fwrite($b, 'B');
echo fread($b, 1), fread($a, 1), PHP_EOL;

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

How It Works

  1. Each end can both write and read.
  2. A written on $a is read from $b.
  3. B written on $b is read from $a.
  4. The pair behaves like a tiny private network.

Example 3 — Handle Creation Failure

Check the return value before using the pair.

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
if ($pair === false) {
    echo 'pair failed', PHP_EOL;
    exit;
}
echo 'pair ok, ends=', count($pair), PHP_EOL;
fclose($pair[0]);
fclose($pair[1]);

How It Works

  1. Always compare the result with === false.
  2. A successful pair has exactly two streams.
  3. count($pair) prints 2.
  4. fclose() each end to release resources.

Frequently Asked Questions

It creates two socket streams that are already connected to each other.
It works offline without a listening server, so send/recv demos stay reliable.
STREAM_PF_INET is the most portable choice across Windows and Unix for simple examples.
No. A pair is a private channel. A server from stream_socket_server() listens for external clients.
The function returns false. Check before unpacking the two ends.

Next: stream_socket_recvfrom()

stream_socket_recvfrom() reads up to a given number of bytes from a socket stream and can .

stream_socket_recvfrom() 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