PHP socket_set_blocking() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, socket_set_blocking() switches a stream between blocking mode and non-blocking mode.

It is an alias of stream_set_blocking(). In blocking mode, reads wait until data arrives. In non-blocking mode, reads return immediately even if no data is ready. The same function works on sockets and on many other streams, including fopen('php://temp').

Syntax

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

PHP
socket_set_blocking($stream, false);
CallWhat it does
socket_set_blocking($stream, true)Makes reads and writes wait until they can finish.
socket_set_blocking($stream, false)Turns on non-blocking mode.
stream_set_blocking($stream, false)Same behavior under the canonical stream name.

Pass the open stream and a boolean. true means blocking; false means non-blocking. Teach both socket_set_blocking() and stream_set_blocking(). Check the boolean return value.

Library declarations
PHP
socket_set_blocking(resource $stream, bool $enable): bool

Parameters

ParameterDescription
streamRequired. An open stream resource.
enableRequired. true for blocking mode, false for non-blocking mode.

Return Value

Return ValueDescription
trueThe mode was changed successfully.
falseThe mode could not be changed.

PHP Examples

Example 1 — Turn Blocking Off and On

Use a php://temp stream and toggle blocking mode with the socket_* alias.

PHP
<?php
$stream = fopen('php://temp', 'r+');

var_dump(socket_set_blocking($stream, false));
var_dump(socket_set_blocking($stream, true));

fclose($stream);

How It Works

  1. fopen() opens an offline stream for the demo.
  2. false enables non-blocking mode.
  3. true restores blocking mode.
  4. Each successful change returns true.

Example 2 — Confirm Mode via Metadata

After changing the mode, read the blocked key from stream metadata.

PHP
<?php
$stream = fopen('php://temp', 'r+');
stream_set_blocking($stream, false);

$meta = socket_get_status($stream);
echo $meta['blocked'] ? 'blocking' : 'non-blocking', PHP_EOL;

socket_set_blocking($stream, true);
$meta = stream_get_meta_data($stream);
echo $meta['blocked'] ? 'blocking' : 'non-blocking', PHP_EOL;

fclose($stream);

How It Works

  1. stream_set_blocking() and socket_set_blocking() are aliases.
  2. Metadata key blocked reflects the current mode.
  3. We mix both function families on purpose for teaching.
  4. The second call restores blocking mode.

Example 3 — Default Mode on a Fresh Stream

Show that a new php://temp stream starts in blocking mode before you change anything.

PHP
<?php
$stream = fopen('php://temp', 'r+');
$meta = stream_get_meta_data($stream);
echo 'default: ', $meta['blocked'] ? 'blocking' : 'non-blocking', PHP_EOL;

socket_set_blocking($stream, false);
echo 'after change: non-blocking', PHP_EOL;
fclose($stream);

How It Works

  1. Fresh file-like streams usually start blocked.
  2. Metadata reports that default without changing anything.
  3. socket_set_blocking($stream, false) switches the mode.
  4. Remember to handle empty reads carefully in non-blocking code.

Frequently Asked Questions

It enables or disables blocking mode on a stream. It is an alias of stream_set_blocking().
In blocking mode, operations wait until they can complete. In non-blocking mode, they return right away even if no data is ready.
Yes. Any compatible stream works, so fopen('php://temp') is useful for offline examples.
Both work. stream_set_blocking() is the modern name; socket_set_blocking() appears in older socket tutorials.
Read the blocked key from socket_get_status() or stream_get_meta_data().

Next: socket_set_timeout()

socket_set_timeout() sets how long a stream will wait during a read or write before giving.

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