Content · Web Development · PHP

PHP stream_socket_shutdown() Function: Usage and Examples

Shuts down socket reads, writes or both on a connected stream.

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

In PHP, stream_socket_shutdown() is useful when your application needs a specific operation rather than manually reproducing it. Shuts down socket reads, writes or both on a connected stream.

Stream functions work with PHP stream resources, wrappers, contexts or custom filters. Not every wrapper supports every operation.

What Is stream_socket_shutdown() in PHP?


Shuts down socket reads, writes or both on a connected stream.

Signature:

stream_socket_shutdown($stream, int $mode): bool

Parameters:

  • $stream — An already opened stream resource, not a filesystem path string.
  • $mode — File opening mode, filter mode, or socket operation mode as appropriate to this API.

Return value: bool.

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

$errorCode = 0;
$errorMessage = "";
$server = stream_socket_server("tcp://127.0.0.1:0", $errorCode, $errorMessage);
if ($server === false) { throw new RuntimeException($errorMessage); }
$address = stream_socket_get_name($server, false);
$client = stream_socket_client("tcp://" . $address, $errorCode, $errorMessage, 2);
if ($client === false) { fclose($server); throw new RuntimeException($errorMessage); }
$accepted = stream_socket_accept($server, 2);
if ($accepted === false) { fclose($client); fclose($server); throw new RuntimeException("Accept failed"); }
$shutDown = stream_socket_shutdown($client, STREAM_SHUT_RDWR);
var_dump($shutDown);
fclose($accepted); fclose($client); fclose($server);

The important operation is stream_socket_shutdown(). Shuts down socket reads, writes or both on a connected stream.

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

$errorCode = 0;
$errorMessage = "";
$server = stream_socket_server("tcp://127.0.0.1:0", $errorCode, $errorMessage);
if ($server === false) { throw new RuntimeException($errorMessage); }
$address = stream_socket_get_name($server, false);
$client = stream_socket_client("tcp://" . $address, $errorCode, $errorMessage, 2);
if ($client === false) { fclose($server); throw new RuntimeException($errorMessage); }
$accepted = stream_socket_accept($server, 2);
if ($accepted === false) { fclose($client); fclose($server); throw new RuntimeException("Accept failed"); }
$shutDown = stream_socket_shutdown($client, STREAM_SHUT_WR);
var_dump($shutDown);
fclose($accepted); fclose($client); fclose($server);

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

$errorCode = 0;
$errorMessage = "";
$server = stream_socket_server("tcp://127.0.0.1:0", $errorCode, $errorMessage);
if ($server === false) { throw new RuntimeException($errorMessage); }
$address = stream_socket_get_name($server, false);
$client = stream_socket_client("tcp://" . $address, $errorCode, $errorMessage, 2);
if ($client === false) { fclose($server); throw new RuntimeException($errorMessage); }
$accepted = stream_socket_accept($server, 2);
if ($accepted === false) { fclose($client); fclose($server); throw new RuntimeException("Accept failed"); }
$shutDown = stream_socket_shutdown($accepted, STREAM_SHUT_RD);
var_dump($shutDown);
fclose($accepted); fclose($client); fclose($server);

Practical note: Shuts down socket reads, writes or both on a connected stream.

Important Behavior and Edge Cases


  • Shuts down socket reads, writes or both on a connected stream.
  • 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 MethodBehavior
stream_socket_shutdown()half-closes or shuts down socket I/O directions.
fclose()closes the entire PHP stream resource.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming stream_socket_shutdown() can be used without its required setupProvide the correct file, stream, state, key or object before calling it; Shuts down socket reads, writes or both on a connected stream.
Assuming one socket read returns the whole messageTreat socket input as a stream and frame messages explicitly; read until a known boundary or length.
Omitting timeouts and failure handlingCheck false results, timeouts, and partial writes; close sockets on every error path.

Key Takeaways


  • stream_socket_shutdown() shuts down socket reads, writes or both on a connected stream.
  • 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: Shuts down socket reads, writes or both on a connected stream. Build in error handling and use this function where its specific behavior matches the application requirements.

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