In PHP, stream_socket_pair() is useful when your application needs a specific operation rather than manually reproducing it. Creates a pair of connected local socket streams when supported by the operating system.
Stream functions work with PHP stream resources, wrappers, contexts or custom filters. Not every wrapper supports every operation.
What Is stream_socket_pair() in PHP?
Creates a pair of connected local socket streams when supported by the operating system.
Signature:
stream_socket_pair(int $domain, int $type, int $protocol): array|false
Parameters:
$domain— Socket address family such as STREAM_PF_UNIX.$type— Socket type such as STREAM_SOCK_STREAM.$protocol— Wrapper protocol or socket transport identifier.
Return value: array|false.
Example 1: Basic Usage
This first example shows the method or function in a minimal context. For environment-dependent operations, adapt the required path, stream or connection before running it.
<?php
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($pair[0], "hello");
echo fread($pair[1], 5);
fclose($pair[0]); fclose($pair[1]);
The important operation is stream_socket_pair(). Creates a pair of connected local socket streams when supported by the operating system.
Example 2: Another Input or Scenario
The next example changes the input or surrounding context to show how the operation behaves in a second situation.
<?php
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
fwrite($pair[1], "pong");
echo fread($pair[0], 4);
fclose($pair[0]); fclose($pair[1]);
Compare the inputs and the handling of the return value with the first example. Avoid assuming that filesystem paths, resource states or binary bytes are interchangeable.
Practical Example: Applying the Operation
This example illustrates how the operation fits into a small application workflow. Paths, streams and secrets must be created or provided securely by the calling application.
<?php
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
stream_set_blocking($pair[0], false);
fwrite($pair[1], "ready");
var_dump(fread($pair[0], 5));
fclose($pair[0]); fclose($pair[1]);
Practical note: Creates a pair of connected local socket streams when supported by the operating system.
Important Behavior and Edge Cases
- Creates a pair of connected local socket streams when supported by the operating system.
- Check failure values and exceptions; do not assume every environment supports the same stream wrapper, extension or filesystem capability.
- When handling paths, streams or external input, validate inputs before using results and release resources after use.
Comparison With Related Functions
| Function or Method | Behavior |
|---|---|
stream_socket_pair() | creates two connected local sockets. |
stream_socket_server() | creates a listener for network connections. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Assuming stream_socket_pair() can be used without its required setup | Provide the correct file, stream, state, key or object before calling it; Creates a pair of connected local socket streams when supported by the operating system. |
| Assuming one socket read returns the whole message | Treat socket input as a stream and frame messages explicitly; read until a known boundary or length. |
| Omitting timeouts and failure handling | Check false results, timeouts, and partial writes; close sockets on every error path. |
Key Takeaways
stream_socket_pair()creates a pair of connected local socket streams when supported by the operating system.- Use appropriate input types and verify results before continuing.
- Prefer specialized, safer APIs when the operation concerns untrusted input, encryption or network traffic.
In summary: Creates a pair of connected local socket streams when supported by the operating system. Build in error handling and use this function where its specific behavior matches the application requirements.