Content · Web Development · PHP

PHP socket_write() Function: Usage and Examples

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.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

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


OptionWhat it means in practice
socket_write() Functionsocket_write() sends bytes over a connected socket and reports the number of bytes actually written.
Related approachUse socket_send() when you need explicit send flags.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for socket_write() FunctionA partial write is not a protocol message; repeat writes until the entire framed payload is transmitted.
Assuming a related mechanism has the same behaviorUse 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.

Share this resourceSend this page to someone who may find it useful.