PHP stream_socket_shutdown() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_socket_shutdown() shuts down part or all of a full-duplex socket connection.

Pass STREAM_SHUT_RD to stop receiving, STREAM_SHUT_WR to stop sending, or STREAM_SHUT_RDWR to stop both. It returns true on success. Closing with fclose() still frees the stream; shutdown signals the peer earlier that one direction is done.

Syntax

Call stream_socket_shutdown() with the values you need. Start with this line.

PHP
$ok = stream_socket_shutdown($stream, STREAM_SHUT_WR);
CallWhat it does
stream_socket_shutdown($stream, STREAM_SHUT_RD)Stops further reading on the socket.
stream_socket_shutdown($stream, STREAM_SHUT_WR)Stops further writing on the socket.
stream_socket_shutdown($stream, STREAM_SHUT_RDWR)Stops both reading and writing.

Use STREAM_SHUT_WR when you finished sending but still want to read a reply. Demo with stream_socket_pair() offline: shut down writing on one end, then show the other end reaches EOF.

Library declarations
PHP
stream_socket_shutdown(resource $stream, int $mode): bool

Parameters

ParameterDescription
streamRequired. A connected socket stream.
modeRequired. STREAM_SHUT_RD, STREAM_SHUT_WR, or STREAM_SHUT_RDWR.

Return Value

Return ValueDescription
trueThe shutdown succeeded.
falseThe shutdown failed.

PHP Examples

Example 1 — Shutdown Write on a Pair

Write a message, shut down writing, and let the other end read then hit EOF.

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

fwrite($a, 'bye');
stream_socket_shutdown($a, STREAM_SHUT_WR);

echo fread($b, 3), PHP_EOL;
$more = fread($b, 1);
echo $more === '' || $more === false ? 'eof' : 'data', PHP_EOL;

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

How It Works

  1. Send bye on end $a.
  2. STREAM_SHUT_WR signals that $a will send no more.
  3. End $b still reads the buffered bytes.
  4. A later read sees end-of-file behavior.

Example 2 — Shutdown Both Directions

Use STREAM_SHUT_RDWR and print the boolean result.

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

$ok = stream_socket_shutdown($a, STREAM_SHUT_RDWR);
echo $ok ? 'shutdown ok' : 'shutdown failed', PHP_EOL;

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

How It Works

  1. STREAM_SHUT_RDWR stops both directions on $a.
  2. true means the call succeeded.
  3. The other end may see the connection closing.
  4. Still call fclose() on both streams.

Example 3 — Mode Constant Reminder

Print the three shutdown modes beginners should remember.

PHP
<?php
echo 'STREAM_SHUT_RD=', STREAM_SHUT_RD, PHP_EOL;
echo 'STREAM_SHUT_WR=', STREAM_SHUT_WR, PHP_EOL;
echo 'STREAM_SHUT_RDWR=', STREAM_SHUT_RDWR, PHP_EOL;

How It Works

  1. RD means stop reading.
  2. WR means stop writing.
  3. RDWR means stop both.
  4. Prefer the named constants over raw numbers in real code.

Frequently Asked Questions

It shuts down reading, writing, or both on a connected socket stream without destroying the PHP stream handle yet.
When you finished sending but still want to read a response from the peer.
No. Shutdown signals one or both directions. fclose() releases the stream resource.
Yes. Use stream_socket_pair(), shut down one end, and observe reads on the other.
The shutdown failed, for example if the stream is not a socket or is already closed.

Next: stream_supports_lock()

stream_supports_lock() reports whether an open stream supports locking with flock().

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