socket_write() sends bytes over a connected socket and reports the number of bytes actually written.
What It Does
socket_write() sends bytes over a connected socket and reports the number of bytes actually written. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
The sockets extension; network code must handle partial I/O, blocking and platform error codes.
Syntax, Parameters and Return Value
socket_write(Socket $socket, string $data, ?int $length = null): int|false
Length is an optional byte count. A return value of zero or a partial count does not mean the full message was sent.
Example 1: Basic Behavior
Send and receive a short message using a local socket pair.
<?php
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $pair); [$sender, $receiver] = $pair;
$count = socket_write($sender, 'hello', 5);
echo $count . ':' . socket_read($receiver, 5);
socket_close($sender); socket_close($receiver);
Example 2: Another Case
Handle partial writes by advancing a byte offset until the buffer is sent.
<?php
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $pair); [$a, $b] = $pair;
$payload = 'message';
$offset = 0;
while ($offset < strlen($payload)) {
$written = socket_write($a, substr($payload, $offset));
if ($written === false || $written === 0) throw new RuntimeException('Send stalled');
$offset += $written;
}
echo socket_read($b, strlen($payload));
socket_close($a); socket_close($b);
Practical Example: Putting It to Use
Add a newline delimiter so the receiver can frame complete messages.
<?php
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $pair); [$output, $input] = $pair;
$line = json_encode(['event' => 'ready'], JSON_THROW_ON_ERROR) . '\n';
socket_write($output, $line);
echo socket_read($input, strlen($line));
socket_close($output); socket_close($input);
Important Behavior and Edge Cases
A partial write is not a protocol message; repeat writes until the entire framed payload is transmitted.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| socket_write() Function | socket_write() sends bytes over a connected socket and reports the number of bytes actually written. |
| Related approach | Use socket_send() when you need explicit send flags. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for socket_write() Function | A partial write is not a protocol message; repeat writes until the entire framed payload is transmitted. |
| Assuming a related mechanism has the same behavior | Use socket_send() when you need explicit send flags. |
Key Takeaways
- socket_write() sends bytes over a connected socket and reports the number of bytes actually written.
- A partial write is not a protocol message; repeat writes until the entire framed payload is transmitted.
- Use socket_send() when you need explicit send flags.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.