Content · Web Development · PHP

PHP socket_shutdown() Function: Usage and Examples

socket_shutdown() disables reads, writes, or both on an established socket without necessarily releasing the handle. A half-close can still allow the other direction to operate; use socket_close() to free the.

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

socket_shutdown() disables reads, writes, or both on an established socket without necessarily releasing the handle.

What It Does


socket_shutdown() disables reads, writes, or both on an established socket without necessarily releasing the handle. 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_shutdown(Socket $socket, int $mode = 2): bool

Mode 0 disables reads, 1 disables writes, and 2 disables both. Closing the resource is a separate action.

Example 1: Basic Behavior


Signal that the writing side is finished while preserving a readable peer.

<?php
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $pair); [$a, $b] = $pair;
socket_write($a, 'done');
socket_shutdown($a, 1);
echo socket_read($b, 4);
socket_close($a); socket_close($b);

Example 2: Another Case


Shut down only the receive direction with mode zero.

<?php
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $pair); [$left, $right] = $pair;
$ok = socket_shutdown($left, 0);
var_dump($ok);
socket_close($left); socket_close($right);

Practical Example: Putting It to Use


Distinguish a full shutdown from socket_close(), which releases the handle.

<?php
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $pair); [$client, $server] = $pair;
$ok = socket_shutdown($client, 2);
echo $ok ? 'Both directions closed' : 'Shutdown failed';
socket_close($client); socket_close($server);

Important Behavior and Edge Cases


A half-close can still allow the other direction to operate; use socket_close() to free the handle.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
socket_shutdown() Functionsocket_shutdown() disables reads, writes, or both on an established socket without necessarily releasing the handle.
Related approachUse socket_close() after both sides finish and all pending operations are complete.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for socket_shutdown() FunctionA half-close can still allow the other direction to operate; use socket_close() to free the handle.
Assuming a related mechanism has the same behaviorUse socket_close() after both sides finish and all pending operations are complete.

Key Takeaways


  • socket_shutdown() disables reads, writes, or both on an established socket without necessarily releasing the handle.
  • A half-close can still allow the other direction to operate; use socket_close() to free the handle.
  • Use socket_close() after both sides finish and all pending operations are complete.

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.